Mysql select * FROM table_one WHERE columns_one and columns_two in tables one and 2 have the same data [duplicate]

时间:2021-11-03 03:36:49

This question already has an answer here:

这个问题在这里已有答案:

I'm doing an sql query on two tables in a database. I want to select data from table one only if the data in columns one and columns two on both tables match. Please help.. Tables one and two have the same structure.

我正在对数据库中的两个表进行SQL查询。我想只在两个表中第一列和第二列的数据匹配时才从表一中选择数据。请帮忙..第一和第二表具有相同的结构。

2 个解决方案

#1


1  

That sounds like a job for EXISTS() which will check if a record with the same (column1,column2) exists.

这听起来像EXISTS()的工作,它将检查具有相同(column1,column2)的记录是否存在。

SELECT * FROM Table1 t
WHERE EXISTS(SELECT 1 FROM Table2 s
             WHERE t.column1 = s.column1 and t.column2 = s.column2)

Can also be done with an INNER JOIN :

也可以使用INNER JOIN完成:

SELECT t.* FROM Table1 t
INNER JOIN Table2 s
 ON(t.column1 = s.column1 and t.column2 = s.column2)

#2


0  

I would use INNER JOIN:

我会使用INNER JOIN:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name

Hope this helps you :)

希望这可以帮到你:)

#1


1  

That sounds like a job for EXISTS() which will check if a record with the same (column1,column2) exists.

这听起来像EXISTS()的工作,它将检查具有相同(column1,column2)的记录是否存在。

SELECT * FROM Table1 t
WHERE EXISTS(SELECT 1 FROM Table2 s
             WHERE t.column1 = s.column1 and t.column2 = s.column2)

Can also be done with an INNER JOIN :

也可以使用INNER JOIN完成:

SELECT t.* FROM Table1 t
INNER JOIN Table2 s
 ON(t.column1 = s.column1 and t.column2 = s.column2)

#2


0  

I would use INNER JOIN:

我会使用INNER JOIN:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name

Hope this helps you :)

希望这可以帮到你:)