php - MySQL 选择具有两个 where 条件且计数大于 1 且具有相同列 ID 的行

标签 php mysql count having-clause

在此示例中,结果应为 conversation_id 165337:

enter image description here

这是我目前无法使用的 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
");

关于php - MySQL 选择具有两个 where 条件且计数大于 1 且具有相同列 ID 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21363513/

相关文章:

sql - 如何在 SQL Server 中使用带有框架的窗口函数执行 COUNT(DISTINCT)

sql-server - SQL Server 2005 - 如何比较字段值,如果不同则返回计数,每次出现

php - Android:无法在MYSQL中插入数据

php - 我如何在查询 $r2 中使用变量表 $r1 而不是 php 文件中 FROM 子句中的表名

mysql检索数据给出限制和子限制

mysql - MySQL ENUMS 是打算与 CakePHP 一起使用还是我应该避免使用它们?

java - hive 模拟 : A counter of a type of bee returns unusually high numbers

php - Mysql ORDER BY 使用日期数据行

php - varchar 与文本 - MySQL

mysql - AFTER INSERT 触发器和 INSERT IGNORE/ON DUPLICATE KEY 更新