Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从具有相同id的多个表中检索数据_Mysql_Sql_Join - Fatal编程技术网

Mysql 从具有相同id的多个表中检索数据

Mysql 从具有相同id的多个表中检索数据,mysql,sql,join,Mysql,Sql,Join,我对sql查询有问题。实际上,我想知道两个表的交集。这是我的桌子 Les_Teach +--------+-----------+ | les_id | teach_id | +--------+-----------+ | 10 | 1 | | 10 | 2 | | 10 | 3 | | 11 | 1 | | 11 | 6 | +--------+--------

我对sql查询有问题。实际上,我想知道两个表的交集。这是我的桌子

Les_Teach
+--------+-----------+
| les_id | teach_id  |
+--------+-----------+
|   10    |     1     |
|   10    |     2     |
|   10    |     3     |
|   11    |     1     |
|   11    |     6     |
+--------+-----------+

Class_Les
+--------+-----------+
| c_id   | les_id    |
+--------+-----------+
|   10    |     1     |
|   10    |     3     |
|   11    |     5     |
|   11    |     1     |
|   11    |     6     |
|   11    |     7     |
+--------+-----------+
示例1 les_id和c_id的交点为10

Query Result 1
+--------+-----------+
|   10    |     1     |
|   10    |     3     |
+--------+-----------+
示例2 les_id和c_id的交点为11

Query Result 2
+--------+-----------+
|   11    |     1     |
|   11    |     6     |
+--------+-----------+
选择不同的 莱西德酒店 从…起 第二类 内部连接Les_教学 在lt.les_id=cl.c_id上 其中lt.les_id=10,cl.c_id=10


仅针对les_Teach中的les_id列和Class_les中的c_id列,我如何检索这样的数据?

如果您给出的示例是您想要得到的结果,您可以使用-

对于10:

select x.*
  from les_teach x
  join class_les y
    on x.les_id = y.c_id
   and x.teach_id = y.les_id
where x.les_id = 10;
第11条:

select x.*
  from les_teach x
  join class_les y
    on x.les_id = y.c_id
   and x.teach_id = y.les_id
 where x.les_id = 11;
对于两个表的完全相交,省略where子句

试验-


交叉口的逻辑是什么?你们的例子并没有阐明你们想要做什么。这是两列的内部连接。你想要一个过滤的结果,还是完整的交集?我尝试了内部连接,但没有这样做。不,我不想要完全相交。@MichaelBerkowski您加入的列是否正确?les_id与c_id,teach_id与les_id这是获得所需输出的内容,尽管列名不匹配。请发布您正在使用的查询。