简体   繁体   English

需要帮助理解SQL中的JOINS

[英]Need help in understanding JOINS in SQL

I was asked the below SQL question in an interview. 我在接受采访时被问到以下SQL问题。 Kindly explain how it works and what join it is. 请解释它是如何工作的以及它的加入方式。

Q: There are two tables: table emp contains 10 rows and table department contains 12 rows. 问:有两个表:table emp包含10行,table department包含12行。

 Select * from emp,department;

What is the result and what join it is? 结果是什么,加入的是什么?

It would return the Cartesian Product of the two tables, meaning that every combination of emp and department would be included in the result. 它将返回两个表的笛卡尔积,这意味着empdepartment每个组合都将包含在结果中。

I believe that the next question would be: Blockquote 我相信下一个问题是:Blockquote

How do you show the correct department for each employee? 您如何为每位员工展示正确的部门?

That is, show only the combination of emp and department where the employee belongs to the department. 也就是说,仅显示员工属于departmentempdepartment的组合。

This can be done by: 这可以通过以下方式完成:

SELECT * FROM emp LEFT JOIN department ON emp.department_id=department.id;

Assuming that emp has a field called department_id , and department has a matching id field (This is quite standard in these type of questions). 假设emp有一个名为department_id的字段,而department有一个匹配的id字段(这在这些类型的问题中非常标准)。

The LEFT JOIN means that all items from the left side ( emp ) will be included, and each employee will be matched with the corresponding department. LEFT JOIN意味着将包括左侧( emp )的所有项目,并且每个员工将与相应的部门匹配。 If no matching department is found, the resulting fields from departments will remain empty. 如果未找到匹配的部门,则departments生成的字段将保持为空。 Note that exactly 10 rows will be returned. 需要注意的是整整 10行的将被退回。

To show only the employees with valid department IDs, use JOIN instead of LEFT JOIN . 要仅显示具有有效部门ID的员工,请使用JOIN而不是LEFT JOIN This query will return 0 to 10 rows, depending on the number of matching department ids. 此查询将返回0到10行,具体取决于匹配的部门ID的数量。

The join you specified is a cross join . 您指定的联接是交叉联接 It will produce one row for each combination of records in the tables being joined. 它将为要连接的表中的每个记录组合生成一行。

I'll let you do the math from there. 我会让你从那里做数学。

This will do a cross join I believe, returning 120 rows. 这将做一个交叉连接我相信,返回120行。 One row for each pair-wise combination of rows from each of the two tables. 每个成对组合两个表中每个表的行的一行。

All-in-all a fairly useless join most of the time. 总而言之,大部分时间都是无用的。

You will get all rows from both tables with each row joined together. 您将从两个表中获取所有行,每行连接在一起。

This is known as a Cartesian join and is very bad. 这被称为笛卡尔联合,非常糟糕。

You will get a total of 120 rows. 您将获得总共120行。

This is also the old implied syntax (18 yeasr out of date) and accidental cross joins are a common problem with this syntax. 这也是旧的隐含语法(18年过期)和偶然的交叉连接是这种语法的常见问题。 One should never use it. 一个人永远不应该使用它。 Explict joins are a better choice. Explict连接是更好的选择。 I would have also mentioned this in an interview and explained why. 我也会在一次采访中提到这一点并解释原因。 I also would not have taken the job if they actually used crappy syntax like this because it's very use shows me the database is very likely to be poorly designed. 如果他们真的使用这样糟糕的语法,我也不会接受这个工作,因为它非常有用,这表明数据库很可能设计得很差。

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

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