简体   繁体   English

Mysql - 选择具有链接值的行

[英]Mysql - select rows which have linked values

Sorry for the bad title, do not know how to describe it in one sentence.抱歉标题不好,不知道怎么用一句话形容。

There is the table:有表:

id, user_id, task_id

I need to select user_ids which have records with several task_id Kind of pseudocode我需要选择具有多个 task_id 类型伪代码的记录的 user_ids

Select user_id from tasks wherehas task_id = 1 and task_id = 2

As they say, there are many ways to skin a cat.正如他们所说,有很多方法可以给猫剥皮。 Here is one:这是一个:

To get a list of users that have multiple tasks assigned, and (at minimum) they have task_id 1 AND task_id 2:要获取分配了多个任务的用户列表,并且(至少)他们有 task_id 1 和 task_id 2:

SELECT 
   user_id
FROM user_tasks // or whatever your table is called 
WHERE task_id in (1, 2)
GROUP BY user_id
HAVING COUNT(DISTINCT task_id) = 2

Let us get all the rows where task is is 1 or 2, group them up by user and show only users who have a min task ID that is different to the max task id:让我们获取任务为 1 或 2 的所有行,按用户对它们进行分组,并仅显示最小任务 ID 与最大任务 ID 不同的用户:

SELECT 
 user_id
FROM t 
WHERE task_id in (1, 2)
GROUP BY user_id
HAVING MIN(task_id) <> MAX(task_id)

You need to appreciate in all of this that when you write a WHERE clause, it is evaluated to be true for every individual row.您需要了解所有这些,当您编写 WHERE 子句时,它对每一行都被评估为真。 This means you cannot say WHERE x = 1 AND x = 2 because the value of X can never ever be simultaneously 1 and 2 ON THE SAME ROW.这意味着您不能说WHERE x = 1 AND x = 2因为 X 的值永远不会在同一行同时为 1 和 2。

Instead we use an OR, to get all the user rows that are 1 or 2 and this will give us a mix - some users have multiple rows (the first row is 1 and the next row is 2).相反,我们使用 OR 来获取所有为 1 或 2 的用户行,这会给我们一个混合 - 有些用户有多个行(第一行是 1,下一行是 2)。 Other users have only one row- either a one or a two.其他用户只有一行 - 一或二。

User Task
John 1
Jane 1
Jane 1
Jake 1
Jake 2
Joel 2

When we apply a grouping operation, all those rows are squashed down to one row per user and then things like MIN and MAX can be used and make sense.当我们应用分组操作时,所有这些行都被压缩为每个用户一行,然后可以使用 MIN 和 MAX 之类的东西并有意义。 For a user with only one row his MIN and MAX will be the same number.对于只有一行的用户,他的 MIN 和 MAX 将是相同的数字。 For a user who had 20 rows that are all value 1, they will also be MIN and MAX the same.对于有 20 行值都为 1 的用户,它们的 MIN 和 MAX 也相同。 Only users who have at least one row that is a 1 and another row that is a 2 will have a different min and max:只有至少有一行是 1 和另一行是 2 的用户才会有不同的最小值和最大值:

User MIN MAX
John 1   1
Jane 1   1
Jake 1   2     <-- the only user HAVING MIN <> MAX
Joel 2   2

Min and max is only really useful in cases where you're looking for 2 different values. Min 和 max 仅在您正在寻找 2 个不同值的情况下才真正有用。 In other cases you might look for COUNT DISTINCT that contains the same number as there are elements in the IN list在其他情况下,您可能会查找包含与 IN 列表中的元素相同的数字的 COUNT DISTINCT

I think you mean you want the values of user_id for which there is a task with task_id 1 and a task with task_id 2.我想你的意思是你想要 user_id 的值,其中有一个 task_id 1 的任务和 task_id 2 的任务。

Your solution doesn't work because it's looking for a task which has task_id 1 and task_id 2 (which is impossible)您的解决方案不起作用,因为它正在寻找具有 task_id 1 和 task_id 2 的任务(这是不可能的)

You need:你需要:

select a.user_id from tasks a where a.task_id = 1 inner join 
    tasks b where b.user_id = a.user_id and b.task_id = 2

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

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