2013-10-10 141 views
0

我有2個查詢結合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 = 20 or action = 21)status = 0action = 1status = 1和行的行。

謝謝!

+0

「和行動= 1個狀態= 1」 你確定嗎? – Strawberry

回答

1
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 
+0

這太棒了。當它變得如此簡單時,我正在努力工作。非常感謝! –

2

嘗試使用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 

BTW這樣的:

And (
     action = 21 
     Or action = 20 
    ) 

可以這樣寫

And action IN (21,20) 
0

您可以使用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 
+2

對於使用兩次的命令,您需要將您的查詢包裝在'()'中。 –