2016-12-21 76 views
1

說得這個問題簡單,我用的是follwoing命令:PHP SQL查詢使用陣列

SELECT DISTINCT a.*, b.shortmessage 
FROM `users` a, `rating` b 
WHERE a.userid IN (333333, 101010) 
    AND b.myuserid IN (333333, 101010) 
    AND b.userid LIKE '000000' 

它返回:

id other_columns shortmessage 
3 Guy1    message from guy1 
9 Guy2    message from guy1 
3 Guy1    message from guy2 
9 Guy2    message from guy2 

注意,我可以使用GROUP BY id,它會顯示2行如預期的那樣,但表2的消息在guy2上將會出錯。

我想要什麼:

id other_columns shortmessage 
3 Guy1    message from guy1 
9 Guy2    message from guy2 
+0

您如何定義哪個'shortmessage'應該顯示爲特定* Guy * /'id'? –

回答

0

這些項目都是不同的(看每id不同shortmessage):

id other_columns shortmessage 
3 Guy1    message from guy1 
9 Guy2    message from guy1 
3 Guy1    message from guy2 
9 Guy2    message from guy2 
0

我做了一個快速閱讀了關於INNER從w3s

JOIN

w3s link

解決方案:

SELECT * FROM `users` INNER JOIN `rating` ON users.userid=rating.myuserid WHERE users.userid IN (333333, 101010) AND rating.myuserid IN (333333, 101010) AND rating.userid LIKE '000000'