嘿,我不知道怎么做......
它的作业:列出代码和仅购买“阿根廷”国家的汽车的消费者。
Table sell:
customer | resell | veicle | date | value
---------+---------+-----------+------------+----------
02 | 01 | 03 | 2010-02-05 | 17500.00
04 | 02 | 01 | 2010-01-07 | 28000.00
01 | 03 | 08 | 2010-02-15 | 28000.00
02 | 03 | 02 | 2010-03-12 | 42000.00
03 | 04 | 06 | 2010-02-06 | 11500.00
03 | 02 | 05 | 2010-01-25 | 22100.00
01 | 01 | 04 | 2010-01-21 | 15500.00
表客户:
cod | name | lastname
--------+------------+------------
01 | Jose | Alves
02 | Paulo | Cunha
03 | Maria | DPaula
04 | Joana | Silveria
表格:
cod |manufacturer| model | year | country | price
--------+------------+-----------------+------+-----------+----------
01 | 01 | Gol | 2000 | Brasil | 25000.00
02 | 01 | Golf | 2005 | Argentina | 39000.00
03 | 04 | Ford Ka | 1990 | Brasil | 15000.00
04 | 03 | Corsa Seda | 1995 | Brasil | 12500.00
05 | 04 | Fiesta | 2003 | Argentina | 20000.00
06 | 03 | Corsa Seda | 1995 | Argentina | 10000.00
07 | 05 | Palio | 2002 | Brasil | 15000.00
08 | 05 | Siena | 2006 | Brasil | 26000.00
我想要开始:
SELECT customer.cod, customer.name
FROM sell, customer
WHERE (SELECT cod
FROM veicle
WHERE veicle.country = 'Argentina') = sell.veicle;
只选择阿根廷汽车...... 请指导我回答。
答案 0 :(得分:2)
SELECT c.*
FROM customer c
WHERE EXISTS (
SELECT 1
FROM sell s
JOIN veicle v ON v.cod = s.veicle
WHERE s.customer = c.cod
AND v.country = 'Argentina'
)
AND NOT EXISTS (
SELECT 1
FROM ...
);
我会把剩下的留给你,因为这是作业。填写...
以排除购买非来自阿根廷的车辆的客户 - 与第一个EXISTS
条款非常相似。如果你已经理解了第一个,你可以完成剩下的工作。
答案 1 :(得分:1)
试试这个
SELECT DISTINCT customer.cod, customer.name || customer.lastname FROM customer, veicle, sell WHERE customer.cod = sell.customer AND veicle.cod = sell.veicle AND veicle.country = 'Argentina'
答案 2 :(得分:0)
查看表连接。这样,你可以代替加入子查询(在你的where语句中),你可以加入表格(有点像你的From
子句但你应该指定它加入的内容)然后在你可以放到的地方veicle.country ='阿根廷'。
并且FYI的面板拼写错误。它应该是车辆。
答案 3 :(得分:0)
试试这个:
SELECT c.cod, c.name,c.last_name,v.model
FROM sell as s JOIN customer as c JOIN veicle as v ON s.veicle = v.cod and s.customer = c.cod
WHERE v.country = 'Argentina'
答案 4 :(得分:0)
WITH minima AS (
SELECT DISTINCT s.customer
, MIN(v.country) AS mi
, MAX(v.country) AS ma
FROM sell s
JOIN veicle v ON s.resell = v.cod
GROUP BY s.customer
)
SELECT cu.* FROM customer cu
JOIN minima mm ON mm.customer = cu.cod
WHERE mm.mi = 'Argentina'
AND mm.ma = 'Argentina'
;