简体   繁体   English

mysql从联接查询中的一个表中获取特定的唯一行数

[英]mysql get specific unique row count from one table in a join query

I have a requests and a results table. 我有一个requests和一个results表。 Each with an email_sha256 column. 每个都有一个email_sha256列。

Requests may contain multiple rows with the same email, while emails are unique in the results. 请求可能包含具有相同电子邮件的多行,而电子邮件在结果中是唯一的。 Emails in the results table might not exist in the requests table. 结果表中的电子邮件可能不存在于请求表中。

I want to get 100 results that have an email that exists in the requests table: 我想获得100个结果,并且请求表中存在一封电子邮件:

SELECT results.* FROM results
INNER JOIN requests ON results.email_sha256 = requests.email_sha256
LIMIT 100
  1. This generally works but it may return the same result multiple times if there are multiple requests with the same email. 通常这是可行的,但是如果有多个请求使用相同的电子邮件,则可能会多次返回相同的结果。 Is there some way to make sure that I get 100 unique results instead of duplicates? 有什么方法可以确保获得100个唯一的结果,而不是重复的结果?

  2. The join seems very slow. 加入似乎很慢。 Is there some better way to get the desired result. 有没有更好的方法来获得期望的结果。 eg using EXISTS ? 例如使用EXISTS吗?

1) 1)

This generally works but it may return the same result multiple times if there are multiple requests with the same email. 通常这是可行的,但是如果有多个请求使用相同的电子邮件,则可能会多次返回相同的结果。 Is there some way to make sure that I get 100 unique results instead of duplicates? 有什么方法可以确保获得100个唯一的结果,而不是重复的结果?

Use GROUP BY Docs . 使用GROUP BY Docs

SELECT results.* FROM results
INNER JOIN requests ON results.email_sha256 = requests.email_sha256
GROUP BY results.email_sha256 
LIMIT 100

2) 2)

The join seems very slow. 加入似乎很慢。 Is there some better way to get the desired result. 有没有更好的方法来获得期望的结果。 eg using EXISTS? 例如使用EXISTS?

We can't specifically answer this without an explanation and/or information about the table(s) . 没有表格 的解释和/或信息,我们无法具体回答。 However the most probably answer is that you have not indexed the correct columns. 但是,最可能的答案是您没有索引正确的列。

You should have an index on your JOIN ing column and your GROUP BY column(s). 您应该在JOIN ing列和GROUP BY列上都有一个索引。 In this case that's the same - results.email_sha256 and requests.email_sha256 . 在这种情况下是相同的results.email_sha256requests.email_sha256

That's a good start there's also plenty of more specific Q&A on Stack Overflow on various issues of slow result return by MySQL.... 这是一个好的开始,在堆栈溢出方面,还有很多针对MySQL慢速返回结果等问题的更具体的问答。

With EXISTS: 具有EXISTS:

SELECT r.* FROM results r
WHERE EXISTS (
 SELECT 1 FROM requests WHERE email_sha256 = r.email_sha256
)
LIMIT 100

This returns 100 unique rows rows since email_sha256 is unique in results . 由于email_sha256在结果中是unique ,因此将返回100个唯一的行。

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

相关问题 如何创建MYSQL查询以获取特定结果(从一栏中计算所有唯一值) - How to create MYSQL query to get specific results (count all unique values from one column) MySQL:使用一个查询从第二个表返回行计数 - MySQL: Return row count from second table using one query MySQL JOIN查询-右表中的每一行,左表中的每一行,对包含的数据进行优先级排序 - MySQL JOIN Query - one row from right table for each row left table with prioritizing contained data 从mysql中的另一个表加入一行 - join one row from another table in mysql MySQL从表中获取计数(*)与联接 - MySql get count(*) with a join from a table 加入MYSQL(从一张表计数并从一张表列出) - Join in MYSQL (Count from One table and list from one table) mysql-将整个行从一个表复制到另一个表,并在一个查询中添加带条件的count(*) - mysql - copy entire row to from one table to another and add count(*) with conditions in one query SQL查询以获取表中唯一组合的计数-MySQL - SQL query to get count of unique combinations in the table - MySql SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records 从一个表的MySQL Count,联接到另一个表 - MySQL Count from one table, join to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM