2013-07-01 118 views
-1

我有這個查詢,我需要轉換爲DISTINCT,因爲結果是雙倍我該怎麼做?DISTINCT QUERY ALIAS WITH RESULT DOUBLES

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

添加

GROUP BY x.id_msg

假設id_msg是PK。

UPDATE: 以便檢索所有行並標記一個統計已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 
+0

我不能使用WHERE x.profile_id = sN07X2因爲我需要所有x.profile_id –

+0

好,如果你的原始查詢你有'INNER JOIN',這意味着你會得到所有行'x.profile_id = sN07X'我會更新查詢檢索所有行 – Stephan

+0

完成,請現在檢查 – Stephan