如何在MySQL中跨多个表检索匹配记录 [英] How to retrieve match records across multiple tables in MySQL

查看:253
本文介绍了如何在MySQL中跨多个表检索匹配记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相同的表,例如:

I have two identical tables such as :

table1
col1    col2    col3    col4    col5

table2
col1    col2    col3    col4    col5

我想比较表1和2,并在2个表之间找到相同的行(col1,col2,col3,col4等.).

I want to compare table 1 and 2 and find identical rows (col1, col2, col3, col4 etc..) between the 2 tables.

我认为我们需要使用vtable或类似的东西.

I think we need to use vtable or something similar ..

我尝试了

SELECT * FROM TABLE1 WHERE COL1, COL2, COL3, COL4 IN 
        (SELECT COL1, COL2, COL3, COL4 FROM TABLE2);

它不起作用.. 请帮助:)

It doesn't work .. help please :)

推荐答案

SELECT * 
FROM TABLE1 t
WHERE EXISTS
      ( SELECT *
        FROM TABLE2 tt
        WHERE (COL1, COL2, COL3, COL4) 
            = (t.COL1, t.COL2, t.COL3, t.COL4)
      )
;

这篇关于如何在MySQL中跨多个表检索匹配记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆