"Wildcard" character as a query criteria

Asked

Viewed 1,316 times

1

I am trying to create a query criteria selection engine. For example:

I have a mass of data where the following variables exist:

  1. store code
  2. Brazilian state in which it is located
  3. Brazilian region in which it is located
  4. Flag of products that the store distributes
  5. date of sale
  6. value

The field I wish to return is that of value, provided that one or more of the above criteria has (m) been met(s). I didn’t want to create all varieties of possible combinations between these 5 fields. So I wanted to know:

It is possible to use wildcard characters (type *) as query criteria?

Then I could define the variables as follows:

if(isset($_GET['uf'])){ // Estado brasileiro
    $uf = $_GET['uf'];
} else {
    $uf = "*"; 
}
if(isset($_GET['rg'])){ // Região brasileira
    $rg = $_GET['rg'];
} else {
    $rg = ""; 
}

if(isset($_GET['ini']) && $_GET['fim'] ){ // data incicial
    $ini = $_GET['ini'];
} else {
    $ini = "01/01/2017"; 
    $fim = date("d-m-Y");
}
if(isset($_GET['band'])){ // bandeira
    $band = $_GET['band'];
} else {
    $band = "*"; 
}

And then assemble a single query that can meet any criteria set or not:

$qry = "SELECT sjy_grupo.id_grupo, sjy_empresas.bandeira AS id_bandeira, sjy_bandeira.bandeira
        FROM sjy_bandeira INNER JOIN 
             sjy_grupo INNER JOIN sjy_empresas ON sjy_grupo.id_grupo = sjy_empresas.grupo
        AND sjy_bandeira.id_bandeira = sjy_empresas.bandeira
        WHERE id_grupo = '$grupo'
        AND id_bandeira = '$id_bandeira'
        GROUP BY sjy_grupo.id_grupo, sjy_bandeira.id_bandeira, sjy_bandeira.bandeira";
  • The wildcard characters would be on account of WHERE id_group = '$group' AND id_flag = '$id_flag', right?

  • Exactly Brunno. I want to avoid the trouble of having to create a query for each possibility of combinations between these variables

  • If the answer helped you, please accept it as correct

  • @Runno could not find where accepted as correct

  • just below the title of my answer, on the left, has two arrows and a "check" sign, which turns green when you put the mouse on top.

1 answer

1


The wildcard character exists and is the "%". It should be used in conjunction with the word LIKE in place of the sign of equality (=) (as pointed out in comment.

That said, I think it would be bad to use wildcard characters in this type of query. This is because if by chance you need to use wildcard character on purpose, you will have a problem. Another reason is that it may bring accidentally unwanted values.

In my view, it is better to verify the existence of value in these variables.

$qry = "SELECT sjy_grupo.id_grupo, sjy_empresas.bandeira AS id_bandeira, sjy_bandeira.bandeira
    FROM sjy_bandeira INNER JOIN 
         sjy_grupo INNER JOIN sjy_empresas ON sjy_grupo.id_grupo = sjy_empresas.grupo
    AND sjy_bandeira.id_bandeira = sjy_empresas.bandeira";

if (isset($grupo)) {
    $qry .= " WHERE id_grupo = '$grupo'";
}

if (isset($id_bandeira)){
    $qry .= " AND id_bandeira = '$id_bandeira'";
}

$qry .= " GROUP BY sjy_grupo.id_grupo, sjy_bandeira.id_bandeira, sjy_bandeira.bandeira";
  • 1

    Only to complement, if you prefer to use the joker %, it should be used in conjunction with the word LIKE instead of EQUAL. Ex.: WHERE uf LIKE "%SP%"

  • 1

    @Clebergriff Yes, well pointed out. I edited the answer.

  • Thank you gentlemen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.