简体   繁体   English

使用if条件返回查询结果

[英]Return query result with if condition

I have some table structure like this. 我有一些像这样的表结构。

table property_day 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 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. 在表property_day列中, check_incheck_out用于表示客人在该日期无法办理登机手续或在该日期无法办理退房手续或无法在该日期办理入住和退房手续。 Value 0 indicate guest can check in or check out but value 1 is indicate guest cannot check in or check out. 0表示客人可以办理入住或退房,但值1表示客人无法办理入住或退房手续。

This is the if condition : 这是if条件:

  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 . 如果我在2017-03-27办理登机手续并在2017-03-28退房,并且栏目check_incheck_out在该日期为0 ,我想在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. 如果我在2017-03-27办理登机手续并于2017-03-28退房,2017-03-27栏中的check_in值为1 ,我不想显示所有房间。
  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. 如果我在2017-03-27办理登机手续并于2017-03-28退房,2017-03-28栏的check_out值为1 ,我也不想显示所有房间。
  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. 如果我在2017-03-27办理登机手续并于2017-03-29退房,并且2017-03-28栏中的check_incheck_out值为1 ,我想显示所有房间。
  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. 如果我在2017-03-27办理登机手续并于2017-03-29退房,并且2017-03-28栏中的check_incheck_out值为0 ,我还想显示所有房间。

This is table structure for condition number 1 : 这是条件号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 : 这是条件号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 : 这是条件号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 : 这是条件号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 : 这是条件号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. 谢谢。

Please use mysql CASE... THEN ... END statement to get the result 请使用mysql CASE ... THEN ... END语句来获得结果

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

The IF() mysql syntax goes as this: IF() mysql语法如下:

IF(expression , true, false); IF(表达式,真,假);

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: 我没有将check_incheck_out的条件包含在值1和0中,因为它们令人困惑并导致我不知道(在我理解你的条件的情况下),但如果你坚持,你可以添加它们:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM