Select mysql 行,其中存在相同条件的 2 个不同值

Select mysql rows where there exist 2 different values for same condition

我有以下数据集:

Table 1

first_value | second_value

相同 | 1

相同 | 2

不同 1 |1

不同2 |2

我想从这个table中得到的是相同的,因为'same'对于“1”和“2”都存在。 different1 仅存在于 1,different2 仅存在于 2,因此它们未被选中...这可能吗?非常感谢您的帮助...

您可以将 group byhaving 子句一起使用。

SELECT first_value
from Table1
where second_value in (1,2)
group by first_value
having count(*) =2

根据雷达的回答和您的评论,您正在使用 php 并且已经知道这些数字:

$ids = array(1,2);//You probably already have an array holding your numbers

if(is_array($ids) && count($ids) >0) {

    $query = "SELECT col1 ".
             "FROM table1 ".
             "WHERE col2 IN (".join(",", $ids).") ".
             "GROUP BY col1 ".
             "HAVING COUNT(*) = ".count($ids);
}

如果您使用参数化查询,它看起来当然会有些不同。