【问题标题】:Is there a way to get duplicate values in a column with specific values in another column? [duplicate]有没有办法在另一列中获取具有特定值的列中的重复值? [复制]
【发布时间】:2020-07-02 16:34:40
【问题描述】:

我有一个这样的 sql 表:

TABLE: Info

ID      |   JOB    |  Value
--------|----------|------------------
1       |   leader |  3
2       |   Host   |  212
1       |   User   | 4
2       |   leader |  5
2       |   Host   |  4

我正在尝试从 Value 列中获取重复 ID 必须包含 3 和 4 的行。 我目前正在使用这个查询:

  select * from info where value = 3 and 4
   group by id having count(*) > 1;

我使用此查询的结果

ID     |   JOB    |  Value
--------|----------|------------------
null    |   null   |  null

而我的预期结果应该是:

ID      |   JOB    |  Value
--------|----------|------------------
1       |   leader |  3
1       |   User   | 4

请帮忙,因为我不确定我做错了什么

【问题讨论】:

  • 语法错误where value = 3 and 4尝试where value = 3 and value = 4
  • 如果您按id 分组,那么您将不会得到具有相同id 的2 行

标签: mysql sql select relational-division


【解决方案1】:

您的预期结果样本与您的问题描述不一致.. 值 3,4 的重复行是使用选择的

select id
from info
where value in  (3,4)
group by id 
having count(id)>1

然后

select  * 
from  info 
inner join  (
    select id
    from info
    where value in  (3,4)
    group by id 
    having count(id)>1
) t on t.id = info.id

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 2019-09-28
    • 2013-07-05
    • 2017-03-25
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多