繁体   English   中英

将2个查询合并为一个查询

[英]Combining 2 queries into one query

我有2个查询

Select * From wp_booking_transaction
Where DATE(launched) >= "2013-10-10" And DATE(launched) <= "2013-11-10" 
And action = 1 status = 1 And type = 2 And student_id = 81569 
Order by launched desc

Select * From wp_booking_transaction 
Where (DATE(launched) >= "2013-10-10" And DATE(launched) <= "2013-11-10" 
And status = 0 And type = 2 And student_id = 81569) And (action = 21 Or action = 20) 
Order by launched desc

需求:

获取具有action = 1status = 1行和具有(action = 20 or action = 21)status = 0

谢谢!

尝试使用UNION

Select 
    * 
From 
    wp_booking_transaction 
Where 
    DATE(launched) >= "2013-10-10" 
    And DATE(launched) <= "2013-11-10" 
    And action = 1 
    And status = 1 
    And type = 2 
    And student_id = 81569 

UNION ALL

Select 
    * 
From 
    wp_booking_transaction 
Where (
        DATE(launched) >= "2013-10-10" 
        And DATE(launched) <= "2013-11-10" 
        And status = 0 
        And type = 2 
        And student_id = 81569
    ) 
    And (
        action = 21 
        Or action = 20
    ) 
Order by launched desc

顺便说一句

And (
        action = 21 
        Or action = 20
    ) 

可以写成

And action IN (21,20)
SELECT   *
FROM     wp_booking_transaction
WHERE    /* Conditions that appeared in both original queries */
         DATE(launched) >= "2013-10-10" 
AND      DATE(launched) <= "2013-11-10"
AND      type = 2 
AND      student_id = 81569
AND      /* Conditions that are different between the two queries */
         ((action = 1 AND status = 1) OR (action IN(20, 21) AND status = 0))
ORDER BY launched DESC

您可以使用union all | 在两个查询之间是不同的

Select * From wp_booking_transaction
Where DATE(launched) >= "2013-10-10" And DATE(launched) <= "2013-11-10" 
And action = 1 status = 1 And type = 2 And student_id = 81569 
union all
Select * From wp_booking_transaction 
Where (DATE(launched) >= "2013-10-10" And DATE(launched) <= "2013-11-10" 
And status = 0 And type = 2 And student_id = 81569) And (action = 21 Or action = 20) 
Order by launched desc

暂无
暂无

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

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