select 列的值基于另一列的另外两个值

select columns' values based on another two values from another column

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

为了详细说明,我有一个 rental table, inventory table, customer table, 我想通过 customer_idcustomer table 关联到 rental table 并将 rental table 关联到 inventory table by inventory_id,最终目的是生成指定customer_id.

first_namelast_name个客户

来自

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

不过我想更进一步获得以下 table

 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

即通过检查store_id='1'和[=31=都出现customer_id来获取列值customer_idfirst_namelast_name ].我知道我需要做 COUNT (DISTINCT customer_id) 但我不知道如何申请才能实现它。非常感谢任何帮助!

您是在寻找这两家店的客户吗?一种方法使用 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;

你可以试试下面的-

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