在下面的示例中,我将如何实现从不同表中选择的SQL查询。
SalesCodeA
----------------------------------------------------
ID | EMPLOYEE NAME | EMPLOYEE CODE || EMPLOYEE SALES
1 STEVE JOBS A 1000
SalesCodeB
----------------------------------------------------
ID | EMPLOYEE NAME | EMPLOYEE CODE || EMPLOYEE SALES
1 BILL GATES B 2000
SalesCodeC
----------------------------------------------------
ID | EMPLOYEE NAME | EMPLOYEE CODE || EMPLOYEE SALES
1 JOHN APPLE C 3000
我将如何实现基于EmployeeCode获取数据的内容?我尝试使用
SELECT * FROM SalesCodeA, SalesCodeB, SalesCodeC WHERE EmployeeCode ="A";
但它不起作用。
答案 0 :(得分:1)
可能你正在寻找这个(UNION ALL
)
SELECT ID,[EMPLOYEE NAME],[EMPLOYEE CODE],[EMPLOYEE SALES]
FROM TABLE_1
WHERE [EMPLOYEE CODE] = 'A'
UNION ALL
SELECT ID,[EMPLOYEE NAME],[EMPLOYEE CODE],[EMPLOYEE SALES]
FROM TABLE_2
WHERE [EMPLOYEE CODE] = 'A'
UNION ALL
SELECT ID,[EMPLOYEE NAME],[EMPLOYEE CODE],[EMPLOYEE SALES]
FROM TABLE_3
WHERE [EMPLOYEE CODE] = 'A'
同样为每位员工提供单独的表格看起来不是一个合适的设计,因为employee code
或employee ID
将是唯一的,因此为所有员工提供单一表格
答案 1 :(得分:0)
我完全理解你的查询,但我可以建议你使用不同查询的联合,因为所有的表结构是相同的。
SELECT * FROM TABLE_1 WHERE EMP_CODE='A';
UNION
SELECT * FROM TABLE_2 WHERE EMP_CODE='B';
UNION
SELECT * FROM TABLE_3 WHERE EMP_COE='C';
答案 2 :(得分:0)
试试这个:
SELECT * FROM (
SELECT * FROM SalesCodeA UNION ALL
SELECT * FROM SalesCodeB UNION ALL
SELECT * FROM SalesCodeC) t WHERE t.EmployeeCode ="A";
答案 3 :(得分:0)
您可以使用UNION ALL
运算符。确保所有表格具有相同的列数。
SELECT *
FROM
(SELECT * FROM SalesCodeA
UNION ALL
SELECT * FROM SalesCodeB
UNION ALL
SELECT * FROM SalesCodeC ) t
WHERE t.EMP_CODE='A';