简体   繁体   English

SQL Select 语句(来自 2 个不同的表)

[英]SQL Select statement (from 2 different tables)

Heyy I'm new to sql and I'd just like to know if there's a way to retrieve select statements with conditions from other tables.嘿,我是 sql 的新手,我只想知道是否有办法从其他表中检索带有条件的 select 语句。

I want to select all name values that have a number that identifies that they have committed a crime.我想选择所有具有识别他们犯罪的数字的名称值。 I only want to select a name once.我只想选择一个名字一次。

"SELECT distinct * FROM  Table1 WHERE number LIKE table2.number "

Are you looking for IN ?您在寻找IN吗?

SELECT t1.*
FROM Table1 t1
WHERE t1.number IN (SELECT t2.number FROM table2 t2 t2.number);

Under most circumstances, the rows in a table should be unique.在大多数情况下,表中的行应该是唯一的。 So, you don't need SELECT DISTINCT .所以,你不需要SELECT DISTINCT The DISTINCT can add a considerable amount of overhead to such a query. DISTINCT可以为此类查询增加大量开销。

You can able to use INNER JOIN like below,您可以像下面一样使用 INNER JOIN,

select tbl1.Name from tableOne tbl1
inner join tableTwo tbl2 ON tbl1.commonKey = tbl2.commonKey
where tbl1.columnName = 'any value'

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

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