Duplicate rows after a MySQL query

user10065337

I have 2 tables:

___Rooms

|--------|------------|
| ROO_Id | ROO_Number |
|--------|------------|
| 1      | 101        |
| 2      | 102        |
| 3      | 103        |
| 4      | 104        |
| 5      | 105        |
|--------|------------|

___Bookings

|--------|------------|------------|------------|-------------------|
| BOO_Id | BOO_RoomId | BOO_DateCI | BOO_DateCO | BOO_ArrivalStatus |
|--------|------------|------------|------------|-------------------|
| 1      | 1          | 2018-07-03 | 2018-07-05 | checkout          |
| 2      | 2          | 2018-07-16 | 2018-07-17 | checkin           |
| 3      | 3          | 2018-07-14 | 2018-07-16 | none              | 
| 4      | 5          | 2018-07-14 | 2018-07-18 | checkin           | 
|--------|------------|------------|------------|-------------------|

My goal is to have the following report:

The date of the report is today : 2018-07-16.

|------------|----------------|-------------------|
| ROO_Number | BOO_LiveStatus | BOO_ArrivalStatus |
|------------|----------------|-------------------|
| 101        | no             | checkout          |
| 102        | in-house       | checkin           |
| 103        | in-house       | none              |
| 104        | no             | 0                 |
| 105        | in-house       | checkin           |
|------------|----------------|-------------------|

I put a SQLFidde here : http://sqlfiddle.com/#!9/854079/5


Actually, I'm very near but I have a little problem.

In my report I need one row per room in ___Rooms table. But actually, I have more rows (2 for #102).

(1, 2, '2018-07-13', '2018-07-15', 'checkout') should not be display because the today date is not between 2018-07-13 and 2018-07-15.


My last try was this one:

SELECT 
    ROO_Number, 
    IF(BOO_DateCI <= '2018-07-16' AND BOO_DateCO >= '2018-07-16', 'in-house', 'no')
        AS BOO_LiveStatus,
    IFNULL(BOO_ArrivalStatus, '0') 
        AS BOO_ArrivalStatus
FROM ___Rooms
LEFT JOIN ___Bookings
    ON ___Rooms.ROO_id = ___Bookings.BOO_RoomId
ORDER BY ROO_Number

Could you please help me please ?

Thanks a lot.

Barmar

Add the date check to the ON clause.

SELECT 
    ROO_Number, 
    IF(BOO_DateCI <= '2018-07-16' AND BOO_DateCO >= '2018-07-16', 'in-house', 'no')
        AS BOO_LiveStatus,
    IFNULL(BOO_ArrivalStatus, '0') 
        AS BOO_ArrivalStatus
FROM ___Rooms
LEFT JOIN ___Bookings
    ON ___Rooms.ROO_id = ___Bookings.BOO_RoomId 
        AND CURDATE() BETWEEN ___Bookings.BOO_DateCI AND ___Bookings.BOO_DateCO
ORDER BY 
  ROO_Number

You need to do it in ON rather than WHERE because putting it in WHERE will filter out all the rooms with no bookings at all (since the columns will be NULL).

SqlFiddle

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Remove duplicate rows in MySQL

分類Dev

Delete duplicate rows in mysql

分類Dev

mysql query to update duplicate entries

分類Dev

Query to find duplicate timestamps MySQL

分類Dev

Rearrange DB query result in array for duplicate rows

分類Dev

Eliminating Duplicate Rows after Modifying Stored Procedure

分類Dev

MySQL removing Duplicate Rows with Primary Key

分類Dev

How to delete duplicate rows from mysql

分類Dev

mysql query returning double rows

分類Dev

Insert multiple rows by one mysql query

分類Dev

Cumulative sum for duplicate rows

分類Dev

Get rows with a duplicate value

分類Dev

How to get number of rows by select query using mysql

分類Dev

Is there a limit on the number of rows I can update via an IN clause of a MySQL query?

分類Dev

MySQL query to update multiple rows based on a column using php

分類Dev

MySql query to get rows with only one connected row

分類Dev

Mysql: Select after Insert failing to find rows just inserted

分類Dev

mysql query to count no of rows in joining three tables and count rows of one table

分類Dev

MySQL Workbench: Insert rows after checking data in 2 rows, loop on whole table

分類Dev

How do I sum column after count in mysql query

分類Dev

Email mysql query after multi page php form

分類Dev

VBA Excel Removing duplicate rows

分類Dev

Python pandas: flag duplicate rows

分類Dev

Duplicate rows based on its data

分類Dev

How do you apply CSS to odd/even visible rows after a javascript function has been used to filter out some rows? (incorrectly flagged as duplicate)

分類Dev

Laravel, Mysql duplicate entry

分類Dev

MySQL Update if duplicate row

分類Dev

Mysql Duplicate Control and Whitespace

分類Dev

Merge duplicate values in django query

Related 関連記事

  1. 1

    Remove duplicate rows in MySQL

  2. 2

    Delete duplicate rows in mysql

  3. 3

    mysql query to update duplicate entries

  4. 4

    Query to find duplicate timestamps MySQL

  5. 5

    Rearrange DB query result in array for duplicate rows

  6. 6

    Eliminating Duplicate Rows after Modifying Stored Procedure

  7. 7

    MySQL removing Duplicate Rows with Primary Key

  8. 8

    How to delete duplicate rows from mysql

  9. 9

    mysql query returning double rows

  10. 10

    Insert multiple rows by one mysql query

  11. 11

    Cumulative sum for duplicate rows

  12. 12

    Get rows with a duplicate value

  13. 13

    How to get number of rows by select query using mysql

  14. 14

    Is there a limit on the number of rows I can update via an IN clause of a MySQL query?

  15. 15

    MySQL query to update multiple rows based on a column using php

  16. 16

    MySql query to get rows with only one connected row

  17. 17

    Mysql: Select after Insert failing to find rows just inserted

  18. 18

    mysql query to count no of rows in joining three tables and count rows of one table

  19. 19

    MySQL Workbench: Insert rows after checking data in 2 rows, loop on whole table

  20. 20

    How do I sum column after count in mysql query

  21. 21

    Email mysql query after multi page php form

  22. 22

    VBA Excel Removing duplicate rows

  23. 23

    Python pandas: flag duplicate rows

  24. 24

    Duplicate rows based on its data

  25. 25

    How do you apply CSS to odd/even visible rows after a javascript function has been used to filter out some rows? (incorrectly flagged as duplicate)

  26. 26

    Laravel, Mysql duplicate entry

  27. 27

    MySQL Update if duplicate row

  28. 28

    Mysql Duplicate Control and Whitespace

  29. 29

    Merge duplicate values in django query

ホットタグ

アーカイブ