简体   繁体   English

多项选择的高效SQL查询

[英]Efficient SQL query with multiple selects

I'm currently at university and doing a project where I have queries such as: 我目前在大学里,正在做一个项目,我对此有疑问:

Select * from recent_purchases where customer_id in 
(  select customer_id from 
   customers where name like '%john%'
) 

I'm not sure if this is the most idiomatic way of doing things or if I'm missing the "correct" way of doing it - it certainly feels a bit clunky. 我不确定这是否是最惯用的做事方式,或者我是否错过了“正确的”做事方式-它确实感觉有些笨拙。 I don't really understand joins yet. 我还不太了解加入。 Sorry if a stupid question. 对不起,如果有一个愚蠢的问题。

Use an INNER JOIN instead of a Sub-Select in the WHERE clause: WHERE子句中使用INNER JOIN而不是Sub-Select:

Select * 
from recent_purchases rp 
inner join customers c on c.customer_id = rp.customer_id 
where c.name like '%john%'

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

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