2012-12-03 219 views
0

有沒有一種方法可以結合這兩個MySQL查詢?目前正在進行分類和分頁等工作非常困難。結合MySQL查詢?

SELECT * 
FROM (`lb_sales`) 
WHERE `recurring` = '0' 
AND `created` >= '2012-10-01 00:00:00' 
AND `created` <= '2012-10-30 23:59:59' 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 5; 

SELECT * 
FROM (`lb_sales`) 
WHERE `recurring` = '1' 
AND `created` <= '2012-10-30 23:59:59' 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 5; 
+0

從下面的很多評論可以看出,您的限制可能是一個問題。這兩種類型的記錄中有5種或總共5種工作,還是總計10種工作是重要的?另外你用什麼設置分頁和排序?它是服務器端還是客戶端?它是一個框架還是你自己寫的東西?由於這些收益很小,因此可能會付出更多努力來查看您的分頁設置,然後專注於合併這兩個記錄。 –

+0

@FrankB:'UNION' [由@njk提議](http://stackoverflow.com/a/13686163)克服了「LIMIT」問題。 – eggyal

+0

感謝您的好評。我使用CodeIgniter進行分頁和排序。它應該返回10個結果。 –

回答

0
SELECT * 
FROM (`lb_sales`) 
WHERE ((`recurring` = '0' 
AND `created` >= '2012-10-01 00:00:00' 
AND `created` <= '2012-10-30 23:59:59') 
OR (`recurring` = '1' 
AND `created` <= '2012-10-30 23:59:59')) 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 10; 
+1

原始查詢每個返回最多5條記錄(即最多10條記錄),而該查詢最多返回5條記錄。 – eggyal

+2

好點...我將限制設置爲10,但顯然不能保證結果集將從循環= 0組產生5行,從循環= 1組產生5行。 –

1

嘗試UNION ALL,只要確保列對齊。另外,請勿使用SELECT *。始終指定一個列列表。

SELECT col1, col2, ... 
FROM (`lb_sales`) 
WHERE `recurring` = '0' 
AND `created` >= '2012-10-01 00:00:00' 
AND `created` <= '2012-10-30 23:59:59' 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 5 
UNION ALL 
SELECT col1, col2, ... 
FROM (`lb_sales`) 
WHERE `recurring` = '1' 
AND `created` <= '2012-10-30 23:59:59' 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 5; 
0

嘗試:

SELECT * FROM (`lb_sales`) 
WHERE (`recurring` = '0' AND `created` >= '2012-10-01 00:00:00' 
AND `created` <= '2012-10-30 23:59:59') 
OR (`recurring` = '1' AND `created` <= '2012-10-30 23:59:59')  
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 5; 
+0

原始查詢每個返回最多5條記錄(即總計最多10條記錄),而此查詢最多返回5條記錄。 – eggyal

+0

OP可根據需要更改「限制」。 –

+1

但是,如果他想返回與原始查詢中相同的記錄,則只需調整此「LIMIT」子句即可。假設一個人設置了「LIMIT 10」,但是來自'recurring = 1'的記錄還有5個來自'recurring = 0'中的任何記錄。 – eggyal

0
SELECT * 
FROM (`lb_sales`) 
WHERE (`recurring` = '0' 
AND `created` >= '2012-10-01 00:00:00' 
AND `created` <= '2012-10-30 23:59:59' 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2') 
OR (`recurring` = '1' 
AND `created` <= '2012-10-30 23:59:59' 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2') 
ORDER BY `id` ASC 
LIMIT 5; 

也許是這樣,但不是舒爾如果語法是正確的

+0

原始查詢每個返回最多5條記錄(即總計最多10條記錄),而此查詢最多返回5條記錄。 – eggyal

+0

因爲您必須在末尾更改限制 –

+0

但是,如果他要返回與其原始查詢中的記錄相同,只是調整此「LIMIT」條款不會達到此目的。假設一個人設置了「LIMIT 10」,但是來自'recurring = 1'的記錄還有5個來自'recurring = 0'中的任何記錄。 – eggyal

1
SELECT * 
FROM (`lb_sales`) 
WHERE 
(
`recurring` = '0' 
AND `created` >= '2012-10-01 00:00:00' 
AND `created` <= '2012-10-30 23:59:59' 
) 
OR 
( 
`recurring` = '1' 
AND `created` <= '2012-10-30 23:59:59' 
) 
AND `status` = 'pending' 
AND `type` = 'sale' 
AND `account_id` = '2' 
ORDER BY `id` ASC 
LIMIT 5; 

括號是你的朋友年輕padwan。當然,我假設你想要符合任何標準的記錄。

+3

不一樣,你的查詢可以返回5行,他原來的查詢可能返回10行。 –

+0

你是對的,但正如你可以說這是我們都沒有解決的問題。我對原始問題提出了一個問題,看看我們是否可以獲得關於它的更多信息。 –