Return query result with if condition

2019-08-01 12:31发布

问题:

I have some table structure like this.

table property_day

propday_id | date       | property_id | check_in | check_out
------------------------------------------------------
1          | 2017-03-26 | 5           | 0        | 0
2          | 2017-03-27 | 5           | 0        | 0
3          | 2017-03-28 | 5           | 0        | 0
4          | 2017-03-29 | 5           | 0        | 0

table rooms

room_id | name      | property_id
----------------------------
1       | Deluxe    | 5
2       | Superior  | 5
3       | Executive | 5

I want to show all that room with if condition inside query. I show that room with date check in and date check out.

In table property_day column check_in and check_out is used for indicate guest cannot check in on that date or cannot check out on that date or cannot check in and check out on that date. Value 0 indicate guest can check in or check out but value 1 is indicate guest cannot check in or check out.

This is the if condition :

  1. If I check in on 2017-03-27 and check out on 2017-03-28 and value in column check_in and check_out is 0 on that date I want to show all room in property_id 5.
  2. If I check in on 2017-03-27 and check out on 2017-03-28 and value in column check_in on 2017-03-27 is 1, I doesn't want to show all room.
  3. If I check in on 2017-03-27 and check out on 2017-03-28 and value in column check_out on 2017-03-28 is 1 , I also doesn't want to show all room.
  4. If I check in on 2017-03-27 and check out on 2017-03-29 and value in column check_in and check_out in 2017-03-28 is 1, I want to show all room.
  5. If I check in on 2017-03-27 and check out on 2017-03-29 and value in column check_in and check_out in 2017-03-28 is 0, I also want to show all room.

This is table structure for condition number 1 :

propday_id | date       | property_id | check_in | check_out
------------------------------------------------------
1          | 2017-03-26 | 5           | 0        | 0
2          | 2017-03-27 | 5           | 0        | 0
3          | 2017-03-28 | 5           | 0        | 0
4          | 2017-03-29 | 5           | 0        | 0

This is table structure for condition number 2 :

propday_id | date       | property_id | check_in | check_out
------------------------------------------------------
1          | 2017-03-26 | 5           | 0        | 0
2          | 2017-03-27 | 5           | 1        | 0
3          | 2017-03-28 | 5           | 0        | 0
4          | 2017-03-29 | 5           | 0        | 0

This is table structure for condition number 3 :

propday_id | date       | property_id | check_in | check_out
------------------------------------------------------
1          | 2017-03-26 | 5           | 0        | 0
2          | 2017-03-27 | 5           | 0        | 0
3          | 2017-03-28 | 5           | 0        | 1
4          | 2017-03-29 | 5           | 0        | 0

This is table structure for condition number 4 :

propday_id | date       | property_id | check_in | check_out
------------------------------------------------------
1          | 2017-03-26 | 5           | 0        | 0
2          | 2017-03-27 | 5           | 0        | 0
3          | 2017-03-28 | 5           | 1        | 1
4          | 2017-03-29 | 5           | 0        | 0

This is table structure for condition number 5 :

propday_id | date       | property_id | check_in | check_out
------------------------------------------------------
1          | 2017-03-26 | 5           | 0        | 0
2          | 2017-03-27 | 5           | 0        | 0
3          | 2017-03-28 | 5           | 0        | 0
4          | 2017-03-29 | 5           | 0        | 0

This is the query which I already try but doesn't give result what I want.

SELECT p.*, r.*
FROM property_day p
JOIN rooms r on p.property_id = r.property_id
WHERE p.property_id = 5 AND p.check_in = 0 OR p.check_out = 0

That's simple query because I doesn't know to make some query with that if condition.

Please anyone help me to do that. Thank you.

回答1:

Please use mysql CASE... THEN ... END statement to get the result

http://www.mysqltutorial.org/mysql-case-statement/



回答2:

The IF() mysql syntax goes as this:

IF(expression , true, false);

You can create a statement like the example below. I haven't tested it but if it works, then good. You can re-rwite it though if you figured out a best way for your desired result. I didn't include the condition of check_in and check_out for value 1 and 0 because they are confusing and results to I don't know (in my pov of understanding your if conditions), but you can add them if you insist:

SELECT p.property_id, 
    IF(p.date = '2017-03-27' OR p.date = '2017-03-28',
        IF(p.property_id = 5, 
            r.name,
            '---'
        ), 
        IF(p.date = '2017-03-29', 
            r.name,
            '---'
        )
    ) AS your_result
FROM property_day p
JOIN rooms r on p.property_id = r.property_id
WHERE p.date >= '2017-03-27' AND p.date <= '2017-03-29'