0

我有一个几乎正确的 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);

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

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

4

3 回答 3

2
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);
于 2013-05-20T21:47:02.387 回答
0

会不会

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) 
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)
LIMIT 1;

不可能?

稍微澄清一下会有所帮助,例如这个查询的最终设计目的是什么。如果它最初设计为返回多行并且您需要它来服务于另一个目的,那么您应该给它一个干净的大修。

于 2013-05-20T21:40:52.423 回答
0

您应该只需要添加另一个 where 子句来消除除 is_primary 为 true 的记录之外的所有内容。

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 
AND c.is_primary = 1
ORDER BY COALESCE(u.username,non_username);

将“true”的正确值替换为 is_primary。

于 2013-05-20T21:49:01.730 回答