根据另一列中的另外两个值选择列的值

时间:2019-05-16 10:13:43

标签: sql subquery distinct relation

我有此数据集,我需要根据列值提取多个列值。

要详细说明,我有一个 rental表,inventory表,customer表, 并且我想将customer的{​​{1}}表与rental表相关,并将customer_id的{​​{1}}表与rental表相关,最终目的是生成inventoryinventory_id的指定first_name客户的客户。

作者

last_name

我已经达到

的结果
customer_id

但是我想更进一步以获得下表

SELECT * 
FROM (
     SELECT 
     inventory.inventory_id,inventory.store_id,
     rental.customer_id,customer.first_name,customer.last_name
     FROM inventory
     INNER JOIN rental ON inventory.inventory_id=rental.inventory_id
     INNER JOIN customer ON rental.customer_id=customer.customer_id
     WHERE inventory.film_id='25') AS data
;

通过检查inventory_id | store_id | customer_id | first_name | last_name --------------+----------+-------------+------------+----------- 124 | 1 | 265 | Jennie | Terry 124 | 1 | 497 | Gilbert | Sledge 124 | 1 | 158 | Veronica | Stone 125 | 1 | 377 | Howard | Fortner 125 | 1 | 163 | Cathy | Spencer 125 | 1 | 141 | Debbie | Reyes 125 | 1 | 558 | Jimmie | Eggleston 126 | 1 | 327 | Larry | Thrasher 126 | 1 | 147 | Joanne | Robertson 126 | 1 | 225 | Arlene | Harvey 126 | 1 | 393 | Philip | Causey 126 | 1 | 425 | Francis | Sikes 127 | 1 | 336 | Joshua | Mark 127 | 1 | 350 | Juan | Fraley 128 | 2 | 96 | Diana | Alexander 128 | 2 | 273 | Priscilla | Lowe 128 | 2 | 163 | Cathy | Spencer 129 | 2 | 498 | Gene | Sanborn 129 | 2 | 360 | Ralph | Madrigal 129 | 2 | 292 | Misty | Lambert 129 | 2 | 578 | Willard | Lumpkin 129 | 2 | 53 | Heather | Morris inventory_id | store_id | customer_id | first_name | last_name --------------+----------+-------------+------------+----------- 124 | 1 | 265 | Jennie | Terry 126 | 2 | 265 | Jennie | Terry 125 | 1 | 163 | Cathy | Spencer 128 | 2 | 163 | Cathy | Spencer 的{​​{1}}的出现来获得列值customer_idfirst_namelast_name。我知道我需要做 customer_id,但我不知道该如何申请。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您在寻找两家商店的顾客吗?一种方法使用exists

with t as (
      SELECT i.inventory_id, i.store_id,
             r.customer_id, c.first_name, c.last_name
      FROM inventory i JOIN
           rental r
           ON i.inventory_id = r.inventory_id JOIN
           customer c
           ON r.customer_id = c.customer_id
      WHERE i.film_id = 25
     )
select t.*
from t
where t.store_id in ( 1, 2) and
      exists (select 1
              from t t2
              where t2.customer_id = t.customer_id and
                    t2.store_id in (1, 2) and
                    t2.store_id <> t.store_id
             );

但是,如果只希望在两个商店中都购买过商品的客户,则可以使用更简单的查询:

SELECT r.customer_id, c.first_name, c.last_name
FROM inventory i JOIN
     rental r
     ON i.inventory_id = r.inventory_id JOIN
     customer c
     ON r.customer_id = c.customer_id
WHERE i.film_id = 25 AND
      r.store_id in (1, 2)
GROUP BY r.customer_id, c.first_name, c.last_name
HAVING COUNT(DISTINCT r.store_id) = 2;

答案 1 :(得分:1)

您可以在下面尝试-

SELECT 
     inventory.inventory_id,inventory.store_id,
     rental.customer_id,customer.first_name,customer.last_name
     FROM inventory
     INNER JOIN rental ON inventory.inventory_id=rental.inventory_id
     INNER JOIN customer ON rental.customer_id=customer.customer_id
     WHERE inventory.film_id='25' and inventory.store_id in (1,2)
     group by nventory.inventory_id,inventory.store_id,
     rental.customer_id,customer.first_name,customer.last_name
     having count(distinct inventory.store_id)=2