How not to select records that are within a date?

Asked

Viewed 339 times

0

I have a table in Mysql with apartment, so:

id |   nome   | data_ja_locado
1  | ap teste | 09/08/2015, 10/08/2015, 11/08/2015

So when the user does a search he wants to find apartments that are available within a space of time. If it put as CHECKIN 10/08/2015 and as CHECKOUT 11/08/2015 was not to appear the "test ap" of the above example.

How to make this consultation?

I tried so:

SELECT * FROM apartamento WHERE data_locado NOT BETWEEN '10/08/2015' AND '11/08/2015'

But he lists the apartment anyway, someone can give me a light?

  • You have dates separated by commas in the field data_ja_locado?

  • I don’t see a good way to do that...

  • @Jorgeb. That’s right!

  • That’s terrible!

  • I imagine I would have to know all the dates inside the checkin and checkout and see if there is any of that date in the field data_ja_locado, but I do not know how

  • I know what you want to do, the problem is that the camp data_ja_locado is not a date is a string with dates, there is no way to do so a search, that I know. You’ll have to pick up all the dates and take care of it in your PHP.

  • @Jorgeb. and what is the correct form?

  • 1

    The right way was to have a table apartamento_datas with all dates per apartment. For example, with the fields id | id_apartamento | data_ocupacao.

  • 1

    The date has to be in American format: 2015-08-10 AND 2015-08-11 and be a DATE field, if you are using a VARCHAR field, or TEXT, you need to fix this.

  • 1

    You’re doing it wrong, instead of putting the dates in a single comma-separated field. Place them in a separate table, in American format and call them through a foreign key.

Show 5 more comments

1 answer

1


How could you get around the problem:
Create an auxiliary table with the rental dates of the apartment called leasing dates. Then instead of putting the dates in a single field, refer to the given by the apartment id, and bring with you the dates relating to this table:

+------------------------------------+
| id_data | id_apto | data_ja_locado |
+------------------------------------+
|    1    |    1    |  2015-08-09    |
+------------------------------------+
|    2    |    1    |  2015-08-10    |
+------------------------------------+
|    3    |    1    |  2015-08-11    |
+------------------------------------+
|    4    |    1    |  2015-08-12    |
+------------------------------------+
|    5    |    1    |  2015-09-01    |
+------------------------------------+
|    6    |    1    |  2015-09-02    |
+------------------------------------+

First you will bring the list of apartments that are not part of your query:

$sql = "SELECT apt.* FROM apartamento apt
        INNER JOIN datas_locacao loc
        ON(loc.id_apto=apt.id_apto)
        WHERE loc.data_ja_locado 
        NOT BETWEEN '2015-08-09' AND '2015-08-11' ";

Where the query will display the other results, as in the example:

$aptos = array(
              0 => array(
                        'id_apto' => 1,
                        'nome'=> 'ap teste'
                        )
             );

Done that, now inside the foreach, make a second query bringing all the dates for this query:

$sql_datas = "SELECT id_data,
                     DATE_FORMAT(data_ja_locado,'%d-%m-%Y') as data_loc
              FROM   datas_locacao
              WHERE  id_apto = '$id_apto' and data_ja_locado 
              NOT    BETWEEN '2015-08-09' AND '2015-08-11' 
              ORDER  BY data_ja_locado ASC ";

This consultation would bring the following result:

+-------------------------+
| id_data |   data_loc    |
+-------------------------+
|    4    |  12/08/2015   |
+-------------------------+
|    5    |  01/09/2015   |
+-------------------------+
|    6    |  02/09/2015   |
+-------------------------+

Where the query will display the other results, as in the example:

$datas_locacao['id_apto']['1'] = array(
                                0 => array(
                                      'id_data' => 4,
                                      'data_loc'=> '12/08/2015'
                                     ),
                                1 => array(
                                      'id_data' => 2,
                                      'data_loc'=> '01/09/2015'
                                     ),
                                2 => array( 
                                      'id_data' => 3,
                                      'data_loc'=> '02/09/2015'
                                     )
                               );

Done this just join and display the results:

foreach ($aptos as $key => $apto) {
          $datas = array();
          foreach ($datas_locacao[$key][$apto['id_apto']] as $data) {
              $datas[] =  $data['data_loc'];         
          }
   echo "<b>Apto: <b/>" . $apto['nome'];
   echo "<b>Período(s) de locação:<b/>" . implode(" - ", $datas)    
}

Exit:

Apt: ap test
Lease period(s): 12/08/2015 - 01/09/2015 - 02/09/2015

  • 1

    That’s it, saved me! I had to use a mirrored database because I couldn’t change the client database, but it worked!

Browser other questions tagged

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