gpt4 book ai didi

mysql - SQL SELECT 来自多个表或 JOIN

转载 作者:可可西里 更新时间:2023-11-01 07:56:14 24 4
gpt4 key购买 nike

我有两个表,我需要在相同的 SELECT 输出中从中获取数据。问题是我需要限制结果的数量。

假设我有一个 ID 列,它在 table1 中是唯一的,但在 table2 中,它有很多行具有该 ID.

现在我只想列出我在 table1 中有多少个不同的 ID 以及存储在 table2 中的一些其他信息。

我怎样才能获得最终显示的所需输出?

为了阐明我的想法,我使用了“信使”数据库作为示例。

表格

T1


Id_thread Date
1 13Dic
2 12Dic

T2


Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother

期望的输出


T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic

最佳答案

我会加入并使用 distinct:

SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread

关于mysql - SQL SELECT 来自多个表或 JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27615543/

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