简体   繁体   English

MySQL 语句根据另一个表从一个表中获取数据

[英]MySQL statement to get data from one table depending on another tabel

I have these two tables我有这两张桌子

在此处输入图像描述

I need a MySQL statement that if a spot has 2 or more rows in the facility table.我需要一个 MySQL 语句,如果一个点在设施表中有 2 行或更多行。 Lets say that I need to find spots that has both shower and toilet.可以说我需要找到既有淋浴又有厕所的地方。

I have tried with something like this我试过这样的东西

select * 
from spot 
where spot.id IN (
                    select spot_id 
                    from facility 
                    where facility LIKE 'shower' 
                    AND facility LIKE 'toilet'
                )

But the subselect doesn't return anything while title can't be both 'shower' AND 'toilet'.但是当标题不能同时是“淋浴”和“厕所”时,子选择不会返回任何内容。

Edit: I got this working编辑:我得到了这个工作

select * from spot where spot.id IN ( select spot_id from facility where facility LIKE 'toilet' ) AND spot.id IN ( select spot_id from facility where facility LIKE 'shower' ) select * 来自 spot.id IN ( select spot_id from facility where facility LIKE 'toilet' ) AND spot.id IN ( select spot_id from facility where facility LIKE 'toilet' ) AND spot.id IN ( select spot_id from factory where facility LIKE 'toilet' ) AND spot.id IN ( Z99938282F04071859941E18F16EFCF'2 show from where facility LIKE Spotid

You have to use OR instead of AND or you can use IN or we can use Join as well.您必须使用 OR 而不是 AND,或者您可以使用 IN,或者我们也可以使用 Join。

select * from spot where spot.id IN (
     select spot_id from facility where facility IN ('shower','toilet'));

or或者

select * from spot where spot.id IN (
     select spot_id from facility where facility LIKE 'shower'
    OR facility LIKE 'toilet');

or或者

select s.id, s.title from spot s join facility f on s.id = f.spot_id where f.facility in ('shower','toilet');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM