2013-07-24 80 views
0

我有一個SQL查詢,我必須從兩個不同的表中獲取值(說a1,a2)。出於簡單的原因,它的工作原理,但因爲我有一個UNION在同一個查詢中有一個重要的原因(從表a2中獲取值放置在a1的列值之間),由於這個原因,我的查詢結果變得很奇怪。這是我的查詢和圖像,它顯示了同樣TICKET_ID的重複條目,我想刪除它,怎麼樣?在兩個表中顯示兩個表與一個UNION

QUERY

SELECT 
RECEIPTS.DATENEW AS DATE, 
TICKETS.TICKETID AS TICKETID, 
PAYMENTS.PAYMENT AS PAYMENT, 
PAYMENTS.TOTAL AS TOTAL, 
CUSTOMERS.NAME AS NAME, 
ADJUSTMENTS.ADJUSTMENT_REASON AS REASON 
FROM RECEIPTS 
INNER JOIN TICKETS ON RECEIPTS.ID = TICKETS.ID 
INNER JOIN PAYMENTS ON RECEIPTS.ID = PAYMENTS.RECEIPT 
INNER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID 
RIGHT JOIN ADJUSTMENTS ON ADJUSTMENTS.CUSTOMER_ID = CUSTOMERS.ID 
WHERE 
(PAYMENTS.PAYMENT = 'debt' 
OR PAYMENTS.PAYMENT = 'debtpaid') 
UNION SELECT 
ADJUSTMENTS.DATE AS DATE, 
ADJUSTMENTS.TICKET_NO AS TICKETID, 
ADJUSTMENTS.ADJUSTMENT_TYPE AS PAYMENT, 
ADJUSTMENTS.ADJUSTMENT_AMOUNT * -1 AS TOTAL, 
ADJUSTMENTS.CUSTOMER_NAME AS NAME, 
ADJUSTMENTS.ADJUSTMENT_REASON AS REASON 
FROM ADJUSTMENTS 
ORDER BY NAME ASC, DATE DESC 

,這裏是它的輸出圖像

輸出

enter image description here

+0

換另一個查詢各地工會,並用它來篩選受騙者? –

+0

你會再詳細一點嗎? – Saqib

回答

0

最後我想通了,現在查詢變得如下

SELECT 
RECEIPTS.DATENEW AS DATE, 
TICKETS.TICKETID AS TICKETID, 
PAYMENTS.PAYMENT AS PAYMENT, 
PAYMENTS.TOTAL AS TOTAL, 
CUSTOMERS.NAME AS NAME, 
(SELECT ADJUSTMENTS.ADJUSTMENT_REASON FROM ADJUSTMENTS WHERE ADJUSTMENTS.ADJUSTMENT_TYPE != 'adjustment' AND ADJUSTMENTS.ADJUSTMENT_REASON != '') AS REASON 
FROM RECEIPTS 
INNER JOIN TICKETS ON RECEIPTS.ID = TICKETS.ID 
INNER JOIN PAYMENTS ON RECEIPTS.ID = PAYMENTS.RECEIPT 
INNER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID 
WHERE 
(PAYMENTS.PAYMENT = 'debt' 
OR PAYMENTS.PAYMENT = 'debtpaid') 
UNION SELECT 
ADJUSTMENTS.DATE AS DATE, 
ADJUSTMENTS.TICKET_NO AS TICKETID, 
ADJUSTMENTS.ADJUSTMENT_TYPE AS PAYMENT, 
ADJUSTMENTS.ADJUSTMENT_AMOUNT * -1 AS TOTAL, 
ADJUSTMENTS.CUSTOMER_NAME AS NAME, 
ADJUSTMENTS.ADJUSTMENT_REASON AS REASON 
FROM ADJUSTMENTS 
ORDER BY NAME ASC, DATE DESC 
1

您可以使用GROUP BY或只是檢查原因列不null

SELECT q.* FROM (

SELECT 
RECEIPTS.DATENEW AS DATE, 
TICKETS.TICKETID AS TICKETID, 
PAYMENTS.PAYMENT AS PAYMENT, 
PAYMENTS.TOTAL AS TOTAL, 
CUSTOMERS.NAME AS NAME, 
ADJUSTMENTS.ADJUSTMENT_REASON AS REASON 
FROM RECEIPTS 
INNER JOIN TICKETS ON RECEIPTS.ID = TICKETS.ID 
INNER JOIN PAYMENTS ON RECEIPTS.ID = PAYMENTS.RECEIPT 
INNER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID 
RIGHT JOIN ADJUSTMENTS ON ADJUSTMENTS.CUSTOMER_ID = CUSTOMERS.ID 
WHERE 
(PAYMENTS.PAYMENT = 'debt' 
OR PAYMENTS.PAYMENT = 'debtpaid') 
UNION SELECT 
ADJUSTMENTS.DATE AS DATE, 
ADJUSTMENTS.TICKET_NO AS TICKETID, 
ADJUSTMENTS.ADJUSTMENT_TYPE AS PAYMENT, 
ADJUSTMENTS.ADJUSTMENT_AMOUNT * -1 AS TOTAL, 
ADJUSTMENTS.CUSTOMER_NAME AS NAME, 
ADJUSTMENTS.ADJUSTMENT_REASON AS REASON 
FROM ADJUSTMENTS 
ORDER BY NAME ASC, DATE DESC) q GROUP BY q.TICKETID 

OR

ORDER BY NAME ASC, DATE DESC) q GROUP BY q.REASON !='' // q.REASON IS NOT NULL 
+0

那麼工作好但是問題是REASON的右欄沒有顯示數據,爲什麼?它是空的! – Saqib

+0

對不起,它沒有工作......它隱藏所有的調整字段,只顯示一個,而我需要顯示所有調整字段有或沒有原因列值:( – Saqib

+0

在這種情況下,您的原始查詢是正確的 –