How to make this SQL request with this particularity?

Asked

Viewed 234 times

0

In this search when the array returns the 4 as the SQL demonstrated below, I need the system to return all the results whose number of dorms and >= 4. You can help me get this result.

I converted the column to INT. It’s ugly in the DB, I didn’t make it, I was ready.

# Selecionando conforme quantidade de dormitorios
$dormi = $_POST['dorm'];
if (isset($_POST['dorm']) && is_array($_POST['dorm']) && count($_POST['dorm']) > 0){

    $where .= " DORMITORIO IN (". implode(', ', $dormi).") AND ";
    if (in_array(4, $_POST['dorm'])){

        $where .= trim($where,' AND ').' OR DORMITORIO >= 4 AND ';

    }

}

This is the SQL record of this search:

SELECT * FROM immovable WHERE 1=1 AND DORMITORIO IN (1, 2, 3, 4)

2 answers

3

You need to do a routine that manages:

SELECT * FROM imovel WHERE dormitorio >= 4

instead of IN. It doesn’t make any sense to use IN in this case.

You can set the form options to 1,2,3 and 4, and use one array in this format to generate the query :

$opcoes = Array( '>= 0', '= 1', '= 2', '= 3', '>= 4' );

so, just do:

$dormi = 0 + @$_POST['dorm']; // A supressão é aceitável neste contexto
$dormi = $dormi>4 ? 4 : $dormi;
$opcoes = Array( '>= 0', '= 1', '= 2', '= 3', '>= 4' );

$where .= ' DORMITORIO '.$opcoes[$dormi];

and the results for 0, 1, 2, 3 and 4 shall be respectively:

WHERE DORMITORIO >= 0
WHERE DORMITORIO = 1
WHERE DORMITORIO = 2
WHERE DORMITORIO = 3 
WHERE DORMITORIO >= 4

It’s still a technical repair 1, then suggest optimizing for your practical case.


1. Gambiarra

  • 1

    @Bacco, I think he sends this data with checkbox, where you can have several options selected.

  • 2

    Maybe, but it’s a little strange that someone wants a house with one or four bedrooms only... and I’ve seen a lot of real estate. But you’ll know, right? :)

  • Yeah, rs... here’s another question from him, I think it’s from the same system. http://answall.com/questions/31319/como-enviar-dados-de-checkbox-para-uma-consulta-com-mysql

  • @Bacco can be strange who is looking for 1 room search for 4 at the same time but it is not so strange to look for 3 and 4. And if I put >=4 eliminate 75% of the search properties as these are 3 rooms at least in this system.

  • 1

    @Marcosvinicius of course, you must do whatever is convenient. It is that in these cases usually the staff adopts the minimum (from 1 upwards, from 2 upwards, from 3 upwards) because who seeks 2, probably will accept a house with 3 bedrooms, but not the reverse. In your specific case, of course you should do the best way that pleases your customer.

  • It is extremely logical what you said !!!

  • 1

    Then the query becomes simpler still, you don’t need the array. WHERE DORMITORIO > '.(0+@$_POST['dorm']); (I wore the best of style XGH to come up with this comment code :D )

Show 2 more comments

1


You can do it like this:

if (isset($_POST['dorm']) && is_array($_POST['dorm']) && count($_POST['dorm']) > 0){

   if (in_array('4', $_POST['dorm'])) {

      $where .= " ( DORMITORIO IN ('".implode("', '", $_POST['dorm'])."') OR DORMITORIO > 4 ) AND ";

   } else $where .= " DORMITORIO IN ('".implode("', '", $_POST['dorm'])."') AND ";

}

So even if the user selects the options 2, 4, the return will be consistent:

SELECT * FROM immovable WHERE 1=1 AND DORMITORIO IN ('2','4') OR DORMITORIO > 4

But in case you wanted to when selecting the option 4, show only the records with dorms greater than or equal to 4, do so:

if (isset($_POST['dorm']) && is_array($_POST['dorm']) && count($_POST['dorm']) > 0){

   if (in_array('4', $_POST['dorm'])) {
      $where .= ' DORMITORIO >= 4 AND ';
   } else {
      $where .= " DORMITORIO IN ('".implode("', '", $_POST['dorm'])."') AND ";
   }

}

Thus, if the user selects the options 2, 4, the return will be:

SELECT * FROM immovable WHERE 1=1 AND DORMITORIO >= 4

Note: I recommend using a class that provides options for creating dynamic filters, like this.

  • Fatal Error: Syntax error or access Violation: 1064 You have a syntax error in your SQL next to 'WHERE 1=1 AND DORMITORIO IN ('4') OR DORMITORIO > 4' on line 1 ...

  • I did not identify any error there, I made a test here and gave normal... Put the generated SQL there to see what is going wrong.

  • That in the bank works: SELECT * FROM imovel WHERE 1 =1 AND DORMITORIO IN ('1', '2', '3', '4') OR DORMITORIO > 4 ... in the request, no ! Must be error in dynamic SQL.

  • Kadu, can you see where the error is? I select the 4 and gives error >>>> http://puu.sh/c3kAx/d13eb5844e.png

  • I made an update there in the code, see if it resolves. At first I see no syntax error... :/

Browser other questions tagged

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