0

嘿嘿不知道怎么弄了。。。。

它的功课:列出代码和购买汽车的消费者仅“阿根廷”国家。

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;

只选择阿根廷汽车...请引导我找到答案。

4

5 回答 5

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条非常相似。如果你理解了第一个,你就可以完成剩下的。

于 2012-06-04T17:39:31.033 回答
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'

于 2012-06-04T17:34:41.200 回答
0

查看表连接。这样,您可以代替执行子查询(您的 where 语句中的内容),而是加入表(有点像您的From子句,但您应该指定它加入的内容),然后在您的 where 您可以放置​​ veicle.country = '阿根廷'。

仅供参考,车辆拼写错误。应该是车辆。

于 2012-06-04T17:34:31.037 回答
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' 
于 2012-06-04T17:40:00.253 回答
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'
        ;
于 2012-06-04T18:52:46.847 回答