简体   繁体   English

mysql选择加入第二行

[英]mysql select join second row

task任务

task_id |  content | date
-------------------------------
1       | task 1   | 2019-11-26
2       | task 2   | 2019-11-27

task_child task_child

id | task_id | type | date_task
--------------------------------
1  | 1       | A    | 2019-11-26
2  | 1       | B    | 2019-11-27
3  | 1       | A    | 2019-11-28
4  | 1       | B    | 2019-11-28
5  | 2       | A    | 2019-11-26
6  | 2       | B    | 2019-11-26
7  | 2       | C    | 2019-11-28

Hi, I have two table as above, how do I join that table in order to get only 2nd row from each task_child?嗨,我有上面的两个表,我如何加入该表以便从每个 task_child 中只获取第二行?

The result should be like below:结果应如下所示:

task_id | content | type | date_task
------------------------------------
1       | task 1  | B    | 2019-11-27
2       | task 2  | B    | 2019-11-26

This should help:这应该有帮助:

   SELECT 
       Task.task_id, 
       Task.content, 
       Task_Child.type,
       Task.task_date
    FROM 
       Task
    INNER JOIN 
       Task_Child 
    ON 
       Task.task_id=Task_Child.task_id;

Hope this helps you.希望这对你有帮助。

If you are running MySQL 8.0, you can use row_number() in a subquery to rank records in the child table within group having the same task_id , and use it as a join filter:如果您运行的是 MySQL 8.0,则可以在子查询中使用row_number()对组内具有相同task_id的子表中的记录进行排名,并将其用作连接过滤器:

select t.task_id, t.content, c.type, c.date_task
from task t
inner join (
    select c.*, row_number() over(partition by task_id order by date_task) rn
    from task_child
) c on c.task_id = t.task_id and c.rn = 2

In earlier versions, this is a bit more complicated.在早期版本中,这有点复杂。 One solution would be to add a join condition with a subquery that ensures that there is just one record in the child table with the same task_id and an earlier date_task than the record currently being joined:一种解决方案是使用子查询添加连接条件,以确保子表中只有一个记录具有相同的task_id和较早的date_task比当前正在加入的记录:

select t.task_id, t.content, c.type, c.date_task
from task t
inner join task_child c 
    on c.task_id = t.task_id 
    and (
        SELECT count(*) 
        from task_child c1 
        where c1.task_id = c.task_id and c1.date_task < c.date_task
    ) = 1

If you want the second status, you can use a correlated subquery:如果你想要第二个状态,你可以使用相关子查询:

select t.*,
       (select tc.type
        from task_child tc
        where tc.task_id = t.task_id
        order by tc.id
        limit 1, 1
       ) as type
from task t;

Given that you want one column, this is probably the most efficient method, regardless of MySQL version.鉴于您想要一列,这可能是最有效的方法,无论 MySQL 版本如何。 For best performance, you want an index on task_child(task_id, id, type) .为了获得最佳性能,您需要在task_child(task_id, id, type)上有一个索引。

By using left join, you can get your required data. 通过使用左联接,您可以获得所需的数据。

SELECT t.task_id, t.content, tc.type, tc.task_date from task t LEFT JOIN task_child tc ON tc.task_id = t.id WHERE tc.id=2

Please refer for joins in more details 请参阅加入的详细信息

http://www.khankennels.com/blog/index.php/archives/2007/04/20/getting-joins/ http://www.khankennels.com/blog/index.php/archives/2007/04/20/getting-joins/

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

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