简体   繁体   English

另一个表中存在的sql查询条目

[英]sql query entries that exist in another table

I have two tables: requests ( email ) , results ( email , data , processed_at ) 我有两个表: requestsemail ), resultsemaildataprocessed_at

I want to get an entry from results that has processed_at = null and an email which exists in requests . 我想从具有processed_at = null results中获取一个条目,并在requests中存在一封电子邮件。 My current approach is to use a join : 我当前的方法是使用联接

select `results`.* from `results` 
inner join `requests` on `requests`.`email` = `results`.`email` 
where `results`.`processed_at` is null limit 1

Unfortunately this is very slow. 不幸的是,这非常慢。 Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点?

You could also use a subquery like: 您还可以使用类似以下的子查询:

SELECT results.*
FROM results
WHERE results.processed_at IS NULL
    AND results.mail IN (SELECT DISTINCT requests.mail FROM requests)
LIMIT 1

However, I'm not sure if it's faster. 但是,我不确定它是否更快。

can you try this query 你可以试试这个查询

select 选择

res .* from res 。*来自

results res, requests req results资源, requests

where req . 其中req email = res . email = res email

and res . res processed_at is null processed_at为null

select results .* from results left join requests on requests . 选择results 。*从results左连接requestsrequests email = results . email = results email where results . email results processed_at is null limit 1 processed_at为空限制1

i am quite sure that this is much faster than inner join. 我很确定这比内部联接要快得多。

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

相关问题 SQL Join查询选择行并检查它们是否存在于另一个表中 - SQL Join query that selects rows and checks if they exist in another table SQL筛选器条目与另一个表中的所有条目匹配 - SQL filter entries which match all entries from another table 将条目从一个MySQL表复制到另一个(如果不存在) - Copy entries from one MySQL table to another if it does not exist MySQL:返回字段,另一个表中不存在相关条目 - MySQL: return field for which no related entries exist in another table 如何使用PHP / mySQL查询一个表而不是另一个表中的条目? - How to query entries that exist in one table and not the other using PHP/mySQL? SQL Query用于获取另一个表中不存在列中值的行 - SQL Query to get rows where values in a column do not exist in another table 是否可以编写一个 SQL 查询来返回另一个表中不存在的值? - Is it possible to write an SQL query that returns the values of something that doesn't exist in another table? 使用SQL,如何编写查询,如果没有条目,则返回null,但列出所有条目(如果存在)? - Using SQL, how do I write a query that returns a null if there are no entries, but lists all entries if any exist? 根据另一个表中的条目从表中查询数据 - Query data from table based of entries in another table 检查表中是否存在所有条目 - Check if all entries exist in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM