简体   繁体   English

一种将 2 个单独的查询组合成一个查询和 1 个表的方法?

[英]A way to combine 2 seperate queries into one query and 1 table?

I want to combine 2 separate queries into one query and be able to sort them into 1 table.我想将 2 个单独的查询组合成一个查询,并能够将它们排序到 1 个表中。

This is the code I am wanting to combine这是我要结合的代码

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;

I also need to distinguish whether or not they are a supplier or a customer in the table.我还需要区分他们是表中的供应商还是客户。 Is there a way to show where the data for that row came from??有没有办法显示该行的数据来自哪里?

I'm hoping to get something like this我希望得到这样的东西

Company Name | Contact Name | Phone       | Table of Origin
-------------+--------------+-------------+-----------------
Target         Steven        111-111-1111  Customer
Factory Name   Connor        222-222-2222  Supplier

I think you just want union all :我想你只想要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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM