【问题标题】:What subquery can I use to filter data depending on strings?我可以使用什么子查询来根据字符串过滤数据?
【发布时间】:2020-02-20 18:31:52
【问题描述】:

我创建了以下表格:

create table Person (

    id_person int,
    name varchar(255),
    primary key (id_person)
);

create table Picture (

    id_picture int,
    id_person int,
    country varchar(255),    //Shows in what country the picture has been taken
    primary key(id_picture),
    foreign key (id_person) references Person (id_person)
);

我想执行一个SELECT 子句来过滤刚刚在UK 和SPAIN 上拍照的人。不在其他国家/地区。

例如:

餐桌人

   id       name
-------------------
   1        Andrew
   2        Javi
   3        Kevin

桌子图片

   id_picture      id_person      country
------------------------------------------------
      1               1           BELGIUM
      2               3           USA
      3               1           UK
      4               2           UK
      5               3           NORWAY
      6               2           SPAIN
      7               1           USA
      8               2           UK
      9               3           DENMARK

如表格所示,只有Person 和id_person = 2 在UK 和SPAIN 中拍照。

如何显示刚刚在UK AND(非 OR)SPAIN 上拍照的Persons?

按照上面的例子,我想得到的输出是:

    id_person      name
-------------------------------
      2            Javi

【问题讨论】:

    标签: mysql sql select subquery


    【解决方案1】:

    使用 distinct count() 尝试如下方式

    select id_person,name from person p join picture p1 on p.id=p1.id_person
     where country in ('uk','spain')
     group by id_person,name
    having count(distinct country)=2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-05
      • 2012-12-06
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      相关资源
      最近更新 更多