MySQL select rows with two where conditions and count greater than 1 with same column ID

StackOverflow https://stackoverflow.com/questions/21363513

سؤال

In this example the result should be conversation_id 165337:

enter image description here

Here is the MySQL I have so far which does not work:

$value1 = $this->_getDb()->fetchCol("
SELECT conversation_id              
FROM xf_conversation_recipient
WHERE user_id = '4465'
AND user_id = '1'
HAVING COUNT(DISTINCT conversation_id) > 1
");

Here is the answer and code which works:

$value1 = $this->_getDb()->fetchCol("
SELECT conversation_id, COUNT(*) c 
FROM xf_conversation_recipient
WHERE user_id IN ('4465','1') 
GROUP BY conversation_id HAVING c > 1
");
هل كانت مفيدة؟

المحلول

Try this then:

SELECT conversation_id, COUNT(*) c 
FROM xf_conversation_recipient  
GROUP BY conversation_id HAVING c > 1;

and in your case:

$value1 = $this->_getDb()->fetchCol("
SELECT conversation_id, COUNT(*) c 
FROM xf_conversation_recipient
WHERE user_id IN ('4465','1') 
GROUP BY conversation_id HAVING c > 1
");

نصائح أخرى

WHERE user_id = '4465' AND user_id = '1' 

correct

WHERE user_id  = '4465' OR user_id = '1' 
$value1 = $this->_getDb()->fetchCol("
SELECT conversation_id FROM xf_conversation_recipient WHERE user_id IN ('4465','1') HAVING COUNT(*) > 1
");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top