gpt4 book ai didi

mysql - SQL 查询返回太多结果

转载 作者:行者123 更新时间:2023-11-29 08:30:54 25 4
gpt4 key购买 nike

我有一个几乎正确的 SQL 查询,但是当用户在数据库中拥有超过 1 架飞机时,他们最终会在返回的结果中出现多行列表:

SELECT  a.this_pay, 
u.username,
u.user_id,
c.type_id,
c.is_primary,
x.typename,
p.pf_city,
p.pf_state,
n.username AS non_username,
n.user_id AS non_user_id,
n.pf_city AS non_pf_city,
n.pf_state AS non_pf_state
FROM (((((jowner_event_attendees a
LEFT JOIN jowner_users u ON a.user_id = u.user_id)
LEFT JOIN jowner_aircraft c ON a.user_id = c.user_id)
LEFT JOIN jowner_aircraft_types x ON x.id = c.type_id)
LEFT JOIN jowner_profile_fields_data p ON a.user_id = p.user_id)
LEFT JOIN jowner_not_users n ON a.user_id = n.user_id)
WHERE a.event_id = 28 ORDER BY COALESCE(u.username,non_username);

我最终得到的列表包含多个条目,适用于拥有多架飞机的任何人,我只希望返回 1 个条目,无论他们在“jowner_aircraft”表中有多少飞机条目。

我也不想大幅更改此查询,因为应用程序的其他部分依赖它,显然它需要我要求的修订,但请尝试在不完全重写的情况下回答它。

最佳答案

SELECT  a.this_pay, 
u.username,
u.user_id,
c.type_id,
x.typename,
p.pf_city,
p.pf_state,
n.username AS non_username,
n.user_id AS non_user_id,
n.pf_city AS non_pf_city,
n.pf_state AS non_pf_state
FROM (((((jowner_event_attendees a
LEFT JOIN jowner_users u ON a.user_id = u.user_id)
LEFT JOIN jowner_aircraft c ON a.user_id = c.user_id AND c.is_primary=1)
LEFT JOIN jowner_aircraft_types x ON x.id = c.type_id)
LEFT JOIN jowner_profile_fields_data p ON a.user_id = p.user_id)
LEFT JOIN jowner_not_users n ON a.user_id = n.user_id)
WHERE a.event_id = 28 ORDER BY COALESCE(u.username,non_username);

关于mysql - SQL 查询返回太多结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16658731/

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