简体   繁体   English

SQL左连接在单个表上

[英]SQL LEFT JOIN ON A SINGLE TABLE

Assume table: 假设表:

MyTable
idPrimary     idPerson     idSchool
-----------------------------------
1             20            6
2             20            3
3             21            2
4             22            6
5             23            6
6             24            3
7             22            7

I would like to find all the persons, who went to school 6 but did not go to school 2 or 3. That means that from the table above, the answer would be students 22 and 23. Student 20 went to school 6 but sadly went to school 3, thereby negating this student. 我想找到所有上过6号学校但没有上2号或3号学校的人。这意味着从上表中可以看出,答案是22和23号学生。20号学生去了6号学校,可惜去了到学校3,从而否定了这个学生。

According to the matrix in question 38549 , I want the LEFT JOIN WHERE B.Key is NULL. 根据问题38549中的矩阵,我希望LEFT JOIN WHERE B.Key为NULL。

(Just out of curiosity is that what is called LEFT OUTER JOIN?) (出于好奇,这就是所谓的LEFT OUTER JOIN?)

The main formula is: 主要公式为:

SELECT <SELECT_LIST> FROM TableA.A LEFT JOIN TableB.B ON A.Key = B.Key WHERE B.Key IS NULL;

Table A would be: 表A为:

SELECT * FROM `MyTable` WHERE `idSchool` = '6';

Table B would be: 表B为:

SELECT * FROM `MyTable` WHERE `idSchool` = '2' OR `idSchool` = '3';

The resultant table should be: 结果表应为:

SELECT `idPerson` FROM SELECT * FROM `MyTable` WHERE `idSchool` = '6' LEFT JOIN SELECT * FROM `MyTable` WHERE `idSchool` = '2' OR `idSchool` = '3' ON `idSchool` = `idSchool` WHERE `idSchool` = NULL;

Sadly MySQL Workbench throws me an error: 可悲的是,MySQL Workbench向我抛出一个错误:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

Near the LEFT JOIN, so basically the SQL engine does not like my TableA. 在LEFT JOIN附近,因此基本上SQL引擎不喜欢我的TableA。 If I wrap TableA in parenthesis, then I get an alias error. 如果将TableA括在括号中,则会出现别名错误。

What is the proper way to issue this query? 发出此查询的正确方法是什么?

Do a LEFT JOIN, checking for the schools not wanted in the ON clause, and check for no match in the WHERE clause:- 进行LEFT JOIN,检查ON子句中不需要的学校,并检查WHERE子句中是否没有匹配项:

SELECT DISTINCT a.idPrimary, a.idPerson, a.idSchool
FROM MyTable a
LEFT OUTER JOIN MyTable b
ON a.idPerson = b.idPerson
AND b.idSchool IN (2,3)
WHERE a.idSchool = 6
AND b.idSchool IS NULL

Use the AS keyword for your table aliases: AS关键字用作表别名:

SELECT <SELECT_LIST>
  FROM TableA AS A
LEFT JOIN TableB AS B ON A.Key = B.Key
 WHERE B.Key IS NULL;

The syntax of a JOIN looks more like: JOIN的语法更像是:

SELECT *
FROM someTable
LEFT JOIN ON someOtherTable ON (someTable.someColumn = someOtherTable.someOtherColumn)
WHERE
someTable.id = 1;

However in your case I think NOT EXISTS looks clearer: 但是,在您的情况下,我认为“ NOT EXISTS看起来更加清晰:

SELECT t1.* 
FROM `MyTable` t1
WHERE t1.`idSchool` = '6'
AND NOT EXISTS (
        SELECT t2.idPerson 
        FROM MyTable t2 
        WHERE t2.idSchool IN (2,3) AND t2.idPerson = t1.idPerson);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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