Question

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
");
Was it helpful?

Solution

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
");

OTHER TIPS

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
");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top