gpt4 book ai didi

一行一对多的mysql

转载 作者:行者123 更新时间:2023-11-29 06:42:35 26 4
gpt4 key购买 nike

我有两个表:

id_c    name    surname
1 frank pulio
2 mark adam
3 peter norton


id_v id_c data_visited note
1 1 2013-10-12 first visited to frank
2 1 2013-10-16 second visited to frank
3 2 2013-10-15 first visited to mark
4 1 2013-10-20 third visited to frank
5 2 2013-10-18 second visited to mark

我会获取带有上次访问数据的客户名称并注明。

例子:

id_c   name   data_visited   note
1 frank 2013-10-20 third visited to frank
2 mark 2013-10-18 second visited to mark

最佳答案

 SELECT t1.`id_c`,c.`name`,t1.latest,t1.`note` FROM `tablea`c

INNER JOIN
(
SELECT t2.`id_c`,t2.latest,t3.`note` from `tableb` t3

INNER JOIN

(SELECT `id_c`,MAX(`data_visited`) AS latest from
`tableb`
GROUP BY(`id_c`))t2
ON (t3.`id_c`=t2.`id_c`) AND (t3.`data_visited`=t2.latest)

)t1
ON(c.`id_c`=t1.`id_c`)

关于一行一对多的mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com