我需要组合2个可能有或没有数据的表,但是我需要一个完整的外连接,其中最后一个表(如果有内容,其中IsActive = 1)显示数据,而不是组合前2个表。
目前有这个:
( SELECT qp.ItemName AS name
, qp.TimeAdded AS created
, '' AS effective
, qp.VendorName AS supplier
, qp.Source AS source
, qp.VendorType AS type
, qp.Price AS cost
, '' AS price
, '' AS markup
, '' AS customer
, '' AS customerListID
, qp.VendorListID AS vendorListID
, '' AS itemListID
FROM wp_quantum_purchases AS qp
WHERE qp.IsActive = 1 AND
NOT EXISTS ( SELECT 1
FROM wp_hunter_quote_parts AS hqp
WHERE qp.ItemName = hqp.ItemName AND
hqp.IsActive = 1 ))
UNION ALL
( SELECT qs.ItemName AS name
, qs.TimeAdded AS created
, qs.SalesDate AS effective
, '' AS supplier
, qs.Source AS source
, '' AS type
, '' AS cost
, qs.Price AS price
, '' AS markup
, qs.CustomerName AS customer
, qs.CustomerListID AS customerListID
, '' AS vendorListID
, '' AS itemListID
FROM wp_quantum_sales AS qs
WHERE qs.IsActive = 1 AND
NOT EXISTS ( SELECT 1
FROM wp_hunter_quote_parts AS hqp
WHERE qs.ItemName = hqp.ItemName AND
hqp.IsActive = 1 ))
UNION ALL
( SELECT hqp.ItemName AS name
, hq.Quote_Date AS created
, hqp.SalesDate AS effective
, hqp.VendorName AS supplier
, hqp.Source AS source
, hqp.VendorType AS type
, hqp.Cost AS cost
, hqp.Price AS price
, CAST(( ( ( CAST(hqp.Price AS DECIMAL(10, 2)) - CAST(hqp.Cost AS DECIMAL(10, 2)) ) / CAST(hqp.Cost AS DECIMAL(10, 2)) ) * 100 ) AS DECIMAL(10, 2)) AS markup
, IFNULL(hq.Customer_FullName, 'N/A') AS customer
, hq.Customer_ListID AS customerListID
, hqp.VendorListID AS vendorListID
, hqp.Item_ListID AS itemListID
FROM wp_hunter_quote_parts AS hqp
LEFT JOIN wp_hunter_quotes AS hq
ON ( hq.id = hqp.QuoteID )
WHERE hqp.IsActive = 1)
ORDER BY NAME ASC;
但这是复制第1和第2表中的数据并显示数据两次。我需要将第1个和第2个表中的数据合并为1(如果存在),但要优先考虑此处的最后一个表(wp_hunter_quote_parts
)作为要显示的内容,如果IsActive = 1
存在于最后一张桌子(wp_hunter_quote_parts
)。但是,如果IsActive = 1
wp_hunter_quote_parts
中ItemName
不存在wp_quantum_purchases
,那么wp_quantum_sales
和LEFT JOIN
就好像它是1行一样。
无法执行wp_quantum_purchases
,因为数据可能存在于wp_quantum_sales
中,但不存在于wp_quantum_sales
中,或wp_quantum_purchases
中的数据可能存在于wp_hunter_quote_parts
中,而wp_hunter_quote_parts
中不存在数据OR数据不能存在于其中任何一个中,并且仅存在于wp_quantum_purchases
中,并且wp_hunter_quote_parts
中甚至可能不存在数据。
所以,基本上,如果ItemName存在于wp_quantum_purchases
且IsActive = 1并且wp_hunter_quote_parts
表中没有ItemName,则从hunter_quote_parts
获取购买数据,否则如果ItemName存在于wp_quantum_sales
中1}}从wp_hunter_quote_parts
获取数据。
如果wp_quantum_sales
中存在ItemName且IsActive = 1并且wp_hunter_quote_parts
表中没有ItemName,则从hunter_quote_parts
获取销售数据,否则(SELECT IFNULL(qp.ItemName, qs.ItemName) AS name, IFNULL(qp.TimeAdded, qs.TimeAdded) AS created, qs.SalesDate AS effective, qp.VendorName AS supplier, qp.Source AS source, qp.VendorType AS type, qp.Price AS cost, qs.Price AS price, CAST((((CAST(qs.Price AS DECIMAL(10,2)) - CAST(qp.Price AS DECIMAL(10,2))) / CAST(qp.Price AS DECIMAL(10,2))) * 100) AS DECIMAL(10,2)) AS markup, qs.CustomerName AS customer, qs.CustomerListID AS customerListID, qp.VendorListID AS vendorListID, '' AS itemListID
FROM wp_quantum_purchases AS qp, wp_quantum_sales AS qs
WHERE (qp.IsActive = 1 OR qs.IsActive = 1)
AND NOT EXISTS (
SELECT 1
FROM wp_hunter_quote_parts AS hqp
WHERE (qp.ItemName = hqp.ItemName || qs.ItemName = hqp.ItemName) AND hqp.IsActive = 1
)
)
UNION ALL
(SELECT hqp.ItemName AS name, hq.Quote_Date AS created, hqp.SalesDate AS effective, hqp.VendorName AS supplier, hqp.Source AS source, hqp.VendorType AS type, hqp.Cost AS cost, hqp.Price AS price, CAST((((CAST(hqp.Price AS DECIMAL(10,2)) - CAST(hqp.Cost AS DECIMAL(10,2))) / CAST(hqp.Cost AS DECIMAL(10,2))) * 100) AS DECIMAL(10,2)) AS markup, IFNULL(hq.Customer_FullName, 'N/A') AS customer, hq.Customer_ListID AS customerListID, hqp.VendorListID AS vendorListID, hqp.Item_ListID AS itemListID
FROM wp_hunter_quote_parts AS hqp
LEFT JOIN wp_hunter_quotes AS hq ON (hq.id = hqp.QuoteID)
WHERE (hqp.IsActive = 1))
ORDER BY name ASC
中存在ItemName来自-std=c++11
的数据。
如何将第一个和第二个表组合在一起,而不是将外连接与另一个表组合?
另一个尝试:
operator()
认为这个可以工作,但似乎它只是继续前进和前进,并且似乎没有完成查询。没有我能看到的错误,但是没有完成......而且这些表非常小,这很奇怪......
答案 0 :(得分:0)
我可能无法完全理解您的问题,但您可以创建前两个表的视图,然后使用第三个表进行外连接。