gpt4 book ai didi

mysql - SQL 查询多列 DISTINCT 在一列上

转载 作者:行者123 更新时间:2023-11-30 01:12:45 27 4
gpt4 key购买 nike

我正在运行如下 SQL 查询,使用 LEFT JOIN 选择每个属性的数据,主要从表 1 ('main_table') 中为每个属性选择数据,如果表 2 也包含,则还在表 2('houses') 中显示数据每个属性的数据。

但是,我只想为每个属性显示 1 个结果。我在选择字段之前尝试了 DISTINCT 的各种结果,但没有成功。

下面是 SQL,还显示了返回数据的示例。例如,128 Mayfield Road 在表 2 中有 2 个条目,因此返回两次,但我只想显示每个房屋一次。

非常感谢

SELECT main_table.housenumber, main_table.streetname, main_table.rent, houses.houseID, houses.path, houses.rooms
FROM main_table
LEFT JOIN houses ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname

533 Portswood Road 57 NULL NULL NULL
35 Ripstone Gardens 70 NULL NULL NULL
67 Kent Road 68 NULL NULL NULL
21 Bealing Close 65 NULL NULL NULL
75 Broadlands Road 76 NULL NULL NULL
7 Gordon Avenue 70 243 images_housing/GOR1.jpg 4
29 Broadlands Road 74 NULL NULL NULL
10 Westbrook Way 65 NULL NULL NULL
328C Burgess Road 85 NULL NULL NULL
10 Thackeray Road 68 NULL NULL NULL
128 Mayfield Road 70 311 images_housing/mayfield1.jpg 4
128 Mayfield Road 67 311 images_housing/mayfield1.jpg 4

最佳答案

也许像这样?

SELECT
main_table.housenumber,
main_table.streetname,
max(main_table.rent)
houses.houseID,
houses.path,
houses.rooms
FROM main_table
LEFT JOIN houses ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
group by
main_table.housenumber,
main_table.streetname,
houses.houseID,
houses.path,
houses.rooms

关于mysql - SQL 查询多列 DISTINCT 在一列上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19335014/

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