簡體   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