[英]MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one
[英]MySQL select rows with two where conditions and count greater than 1 with same column ID
在此示例中,結果應為session_id 165337:
這是到目前為止我無法使用的MySQL:
$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
");
這是有效的答案和代碼:
$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
");
然后嘗試以下方法:
SELECT conversation_id, COUNT(*) c
FROM xf_conversation_recipient
GROUP BY conversation_id HAVING c > 1;
和你的情況:
$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'
正確
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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.