SQL Query返回太多结果

时间:2013-05-20 21:30:36

标签: mysql

我有一个几乎正确的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”中有多少条飞机参赛作品。表

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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;

不可能?

稍微澄清会有所帮助,例如此查询最终要设计的内容。如果它最初设计为返回多行并且您需要它来达到另一个目的,那么您应该给它一个很好的清洁大修。

答案 2 :(得分:0)

你应该只需要添加另一个where子句来消除is_primary为真的记录以外的所有记录。

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。