說我有一些列的表作爲獲得一個SQL列的唯一值
-conversation_id,
-col2,
-user_id。
我想抓取與user_id對應的所有行。例如,可以有20行返回特定的值,但只有3個唯一的conversation_id值。
獲取這些值的最快方法是什麼,而不是通過每一個值來找出唯一的值?
我必須說,我的意思是在PHP中!
謝謝!
說我有一些列的表作爲獲得一個SQL列的唯一值
-conversation_id,
-col2,
-user_id。
我想抓取與user_id對應的所有行。例如,可以有20行返回特定的值,但只有3個唯一的conversation_id值。
獲取這些值的最快方法是什麼,而不是通過每一個值來找出唯一的值?
我必須說,我的意思是在PHP中!
謝謝!
$query = "SELECT DISTINCT conversation_id
FROM conversations
WHERE user_id='2'";
我認爲這可能有效。快速的問題,這將如何與Zend合作?像這樣的工作可以嗎? $ this-> fetchAll($ this-> select() - > where('user_id =?',$ user_id) - > distinct('conversation_id')); ? – user1083320
我從來沒有與zend合作..對不起,但我無法幫助你..你應該嘗試一下,看看它是否有效。 –
SELECT conversation_id, user_id
FROM conversations
GROUP BY user_id, conversation_id
你可以試試這個
select distinct conservation_id,user_id
from table
group by user_id
SELECT DISTINCT conversation_id FROM conversations WHERE user_id = userID
DISTINCT關鍵字用來選擇從結果集只有唯一項目。
在殼體的Zend例如:
$ DB->的select() - >不同的( 'u.conversation_id') - >從(陣列( 'U'=> '用戶')) - > where('u.user_id =?',$ user_id);
請澄清一下您的問題。表格結構,樣本數據和樣本輸出將有所幫助。 – dpp