用多个表模拟 MySQL 中的交集并加入
Emulate an Intersect in MySQL w/ multiple tables and Join
我需要在 MySQL 中模拟 Intersect 查询。
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83649
INTERSECT
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83677
ORDER BY cust_lname, cust_fname;
我尝试过使用 EXISTS 但没有成功。我想我加入错了?
SELECT C.CUST_CODE, CUST_FNAME, CUST_LNAME
FROM LGCUSTOMER C JOIN LGINVOICE I ON C.CUST_CODE = I.CUST_CODE
WHERE EMPLOYEE_ID = 83649
AND EXISTS (SELECT *
FROM LGCUSTOMER C JOIN LGINVOICE I ON C.CUST_CODE = I.CUST_CODE
WHERE EMPLOYEE_ID = 83677)
ORDER BY CUST_LNAME, CUST_FNAME;
表格是:
+--------------+
|客户 |
+------------+
| Cust_code |
| Cust_Fname |
| Cust_Lname |
+------------+
和
+--------------+
| LGINVOICE |
+--------------+
| Cust_code |
| Employee_ID |
+----------------+
我需要一个查询来提取所有具有来自两个唯一员工(83649 和 83677)的发票的客户代码
嗯。 . .想到聚合:
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c JOIN
lginvoice i
ON c.cust_code = i.cust_code
WHERE employee_id IN ( 83649, 83677 )
GROUP BY c.cust_code, cust_fname, cust_lname
HAVING COUNT(DISTINCT employee_id) = 2;
与 INTERSECT
一样,这也删除了重复项。
您可以使用两个子查询之间的内部 joij 来模拟相交
select * from (
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c
JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83649
) t1
INNER JOIN (
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c
JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83677
) t2 on t1.cust_code = t2.cust_code
ORDER BY cust_lname, cust_fname;
我需要在 MySQL 中模拟 Intersect 查询。
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83649
INTERSECT
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83677
ORDER BY cust_lname, cust_fname;
我尝试过使用 EXISTS 但没有成功。我想我加入错了?
SELECT C.CUST_CODE, CUST_FNAME, CUST_LNAME
FROM LGCUSTOMER C JOIN LGINVOICE I ON C.CUST_CODE = I.CUST_CODE
WHERE EMPLOYEE_ID = 83649
AND EXISTS (SELECT *
FROM LGCUSTOMER C JOIN LGINVOICE I ON C.CUST_CODE = I.CUST_CODE
WHERE EMPLOYEE_ID = 83677)
ORDER BY CUST_LNAME, CUST_FNAME;
表格是:
+--------------+
|客户 |
+------------+
| Cust_code |
| Cust_Fname |
| Cust_Lname |
+------------+
和
+--------------+
| LGINVOICE |
+--------------+
| Cust_code |
| Employee_ID |
+----------------+
我需要一个查询来提取所有具有来自两个唯一员工(83649 和 83677)的发票的客户代码
嗯。 . .想到聚合:
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c JOIN
lginvoice i
ON c.cust_code = i.cust_code
WHERE employee_id IN ( 83649, 83677 )
GROUP BY c.cust_code, cust_fname, cust_lname
HAVING COUNT(DISTINCT employee_id) = 2;
与 INTERSECT
一样,这也删除了重复项。
您可以使用两个子查询之间的内部 joij 来模拟相交
select * from (
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c
JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83649
) t1
INNER JOIN (
SELECT c.cust_code, cust_fname, cust_lname
FROM lgcustomer c
JOIN lginvoice i ON c.cust_code = i.cust_code
WHERE employee_id = 83677
) t2 on t1.cust_code = t2.cust_code
ORDER BY cust_lname, cust_fname;