[英]A way to combine 2 seperate queries into one query and 1 table?
我想將 2 個單獨的查詢組合成一個查詢,並能夠將它們排序到 1 個表中。
這是我要結合的代碼
SELECT
t1.CompanyName as 'Customer Company Name',
t1.ContactName, t1.ContactTitle, t1.City, t1.Country, t1.Phone
FROM
Customers t1
WHERE
t1.Country = 'Germany'
ORDER BY
t1.City;
SELECT
t2.CompanyName as 'Supplier Company Name',
t2.ContactName, t2.ContactTitle, t2.City, t2.Country, t2.Phone
FROM
Suppliers t2
WHERE
t2.Country = 'Germany'
ORDER BY
t2.City;
我還需要區分他們是表中的供應商還是客戶。 有沒有辦法顯示該行的數據來自哪里?
我希望得到這樣的東西
Company Name | Contact Name | Phone | Table of Origin
-------------+--------------+-------------+-----------------
Target Steven 111-111-1111 Customer
Factory Name Connor 222-222-2222 Supplier
我想你只想要union all
:
SELECT t1.CompanyName, t1.ContactName, t1.Phone, 'Customers' as which
FROM Customers t1
WHERE t1.Country = 'Germany'
UNION ALL
SELECT t2.CompanyName, t2.ContactName, t2.Phone, 'Suppliers' as which
FROM Suppliers t2
WHERE t2.Country = 'Germany';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.