带有where子句的M​​ysql子查询

时间:2017-05-14 17:24:55

标签: mysql subquery

我需要一些帮助。 我有三个表,名称是hall,hall_quantified_details和hall_hall_quantified_details,它们看起来像这样:

霍尔

hall_id | hall_name
   1        Hall 1
   2        Hall 2

hall_quantified_details

hall_quantified_details_id | name_quantified
           1                       space
           2                       seats

hall_hall_quantified_details

hall_hall_quantified_details_id | hall_id | hall_quantified_details_id | value
          1                         1               1                     100m2
          2                         1               2                     500seats

我想找回hall_id 1的值和值的查询名称,我有查询,但它只返回hall_quantified_details_id的名称...查询看起来像这样:

SELECT p.name_quantified
FROM hall_quantified_details p 
WHERE p.hall_quantified_details_id  IN (
      SELECT pns.hall_quantified_details_id
      FROM hall_hall_quantified_details pns 
      WHERE pns.hall_id = 1
 );

所以我希望从hall_hall_quantified_details结果回到hall_id 1看起来像:space 100;座位500个座位。

1 个答案:

答案 0 :(得分:0)

这是一个简单的连接:

select h2.name_quantified,
    h1.value
from hall_hall_quantified_details h1
join hall_quantified_details h2 on h1.hall_quantified_details_id = h2.hall_quantified_details_id
where h1.hall_id = 1;