-1

我有这个查询,我需要转换为 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
4

1 回答 1

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
于 2013-07-01T08:43:08.857 回答