DISTINCT QUERY ALIAS与结果DOUBLES

时间:2013-07-01 08:41:24

标签: mysql

我有这个查询,我需要转换为DISTINCT,因为结果是双倍我该怎么办?

SELECT x.id_msg, x.id_group, x.profile_id, b.id_group, u.name, u.sirname, u.picup, u.profile_id AS profile_id_users, mp.msg_id,mp.msg_text, mp.occured_at
FROM message_view x
INNER JOIN (

SELECT a.id_msg, a.id_group, a.profile_id
FROM message_view a
WHERE a.profile_id =  'sN07X2'
)b ON x.id_group = b.id_group
INNER JOIN users u ON u.profile_id = x.profile_id
INNER JOIN message_private mp ON mp.msg_id = x.id_msg

1 个答案:

答案 0 :(得分:1)

添加

GROUP BY x.id_msg

假设id_msg是PK。

<强>更新 为了检索所有行并标记一个统计信息profile_id = 'sN07X2'

SELECT 
    x.id_msg, 
    x.id_group, 
    x.profile_id, 
    IF(x.profile_id = 'sN07X2','is profile sN07X2','is not profile sN07X2'), 
    u.name, 
    u.sirname, 
    u.picup, 
    u.profile_id AS profile_id_users, 
    mp.msg_id,
    mp.msg_text, 
    mp.occured_at
FROM message_view x
LEFT JOIN users u 
    ON u.profile_id = x.profile_id
INNER JOIN message_private mp 
    ON mp.msg_id = x.id_msg
GROUP BY
    x.id_msg