gpt4 book ai didi

mysql - 需要我的表为每个 id 仅创建一行

转载 作者:行者123 更新时间:2023-11-29 16:09:35 27 4
gpt4 key购买 nike

我需要连接两个表,它们是:

表 1

t1_id | t1_text | t1_date

表 2

t2_id | t2_text | t2_date | t1_id

我想要得到什么:

t1_text | t1_date | t2_text | t2_date

表必须显示最新的、唯一的 t1 行及其链接的 t2_text 及其 t2_date

这是我到目前为止得到的:

SELECT `table_1`.`t1_text` AS 'Text', `table_1`.`t1_date` AS 't1_date', `table_2`.`t2_text` AS 't2_Text', `table_2`.`t2_date` AS 'Date' 
FROM `table_1`
LEFT JOIN `table_2` ON `table_1`.`t1_id`=`table_2`.`t1_id`
ORDER BY `table_1`.`t1_date` DESC
LIMIT 10

已经接近了,但没有雪茄。我仍然多次出现相同的 t1 行

最佳答案

我不确定我是否清楚地理解您想要的输出是什么。请尝试以下操作:

SELECT news.text AS 'News', news.date AS 'Publish date', 
comments.text AS 'Most Recent Comment', comments.date AS 'Comment Date'
FROM news
JOIN (
SELECT text, MAX(newsId) as newsId, date
FROM comments
GROUP BY newsId
)comments ON news.newsId=comments.newsId
WHERE comments.text IS NOT NULL
ORDER BY news.date DESC LIMIT 10

关于mysql - 需要我的表为每个 id 仅创建一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55384744/

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