1

我有 4 张桌子

台式电脑

code    model   speed   ram hd      cd  price
------------------------------------------------
1       1232    500     64  5.0     12x 600.0000
10      1260    500     32  10.0    12x 350.0000
11      1233    900     128 40.0    40x 980.0000
12      1233    800     128 20.0    50x 970.0000
2       1121    750     128 14.0    40x 850.0000
3       1233    500     64  5.0     12x 600.0000
4       1121    600     128 14.0    40x 850.0000
5       1121    600     128 8.0     40x 850.0000
6       1233    750     128 20.0    50x 950.0000
7       1232    500     32  10.0    12x 400.0000
8       1232    450     64  8.0     24x 350.0000
9       1232    450     32  10.0    24x 350.0000

台式笔记本电脑

code    model   speed   ram hd      price       screen
------------------------------------------------------
1       1298    350     32  4.0     700.0000    11
2       1321    500     64  8.0 9   70.0000     12
3       1750    750     128 12.0    1200.0000   14
4       1298    600     64  10.0    1050.0000   15
5       1752    750     128 10.0    1150.0000   14
6       1298    450     64  10.0    950.0000    12

台式打印机

code    model   color   type    price
----------------------------------------
1       1276    n       Laser   400.0000
2       1433    y       Jet     270.0000
3       1434    y       Jet     290.0000
4       1401    n       Matrix  150.0000
5       1408    n       Matrix  270.0000
6       1288    n       Laser   400.0000

产品

maker   model   Type
-----------------------
A       1232    PC
A       1233    PC
A       1276    Printer
A       1298    Laptop
A       1401    Printer
A       1408    Printer
A       1752    Laptop
B       1121    PC
B       1750    Laptop
C       1321    Laptop
D       1288    Printer
D       1433    Printer
E       1260    PC
E       1434    Printer
E       2112    PC
E       2113    PC

这是我的问题。

找出制造商 B 生产的所有产品(任何类型)的型号和价格。

4

7 回答 7

3
select product.model, price from product, pc
where product.model = pc.model and maker = 'B'
union
select product.model, price from product, laptop
where product.model = laptop.model and maker = 'B'
union
select product.model, price from product, printer
where product.model = printer.model and maker = 'B'
于 2013-09-08T21:25:09.197 回答
1

尝试这个:

select distinct(product.model),pc.price 
from product inner join pc 
on pc.model=product.model
where product.maker='B'

union

select distinct(product.model),laptop.price 
from product inner join laptop
on laptop.model=product.model
where product.maker='B'

union

select distinct(product.model),printer.price
from product inner join printer
on printer.model=product.model
where product.maker='B'
于 2019-02-19T12:58:30.783 回答
0

尝试这个:

SELECT 
  pc.model,
  pc.price,
  p.type
FROM PC
INNER JOIN Product P ON PC.model = p.model AND p.Type = 'PC'
WHERE p.maker = 'B'
UNION ALL
SELECT 
  l.model,
  l.price,
  p.type
FROM Laptop AS l
INNER JOIN Product P ON l.model = p.model AND p.Type = 'LapTop'
WHERE p.maker = 'B'
UNION ALL
SELECT 
  pr.model,
  pr.price,
  p.type
FROM Printer AS pr
INNER JOIN Product P ON Pr.model = p.model AND p.Type = 'Printer'
WHERE p.maker = 'B';

SQL 小提琴演示

这会给你:

| MODEL | PRICE |   TYPE |
--------------------------
|  1121 |   850 |     PC |
|  1121 |   850 |     PC |
|  1121 |   850 |     PC |
|  1750 |  1200 | Laptop |
于 2013-03-25T09:55:51.147 回答
0

目前尚不清楚您是否希望这是单独的列或行,但您可以JOIN使用类似于以下的表来获取单独列中的数据:

select p.model,
  Coalesce(c.price, 0) PC,
  Coalesce(l.price, 0) Laptop,
  Coalesce(t.price, 0) Printer
from product p
left join pc c
  on p.model = c.model
left join laptop l
  on p.model = l.model
left join printer t
  on p.model = t.model
where p.maker = 'B'

请参阅SQL Fiddle with Demo

然后,如果您想将数据放在单独的行而不是列中,您可以使用unpivot函数:

select model, price, col
from
(
  select p.model,
    c.price PC,
    l.price Laptop,
    t.price Printer
  from product p
  left join pc c
    on p.model = c.model
  left join laptop l
    on p.model = l.model
  left join printer t
    on p.model = t.model
  where p.maker = 'B'
) src
unpivot
(
  price
  for col in (pc, laptop, printer)
) un

请参阅带有演示的 SQL Fiddle

于 2013-03-25T10:03:14.330 回答
0
SELECT DISTINCT result.model, result.price FROM 
(
(SELECT PC.model, PC.price FROM PC JOIN Product ON PC.model=Product.model WHERE Product.maker='B')
UNION ALL
(SELECT Laptop.model, Laptop.price FROM Laptop JOIN Product ON Laptop.model=Product.model WHERE Product.maker='B')
UNION ALL
(SELECT Printer.model, Printer.price FROM Printer JOIN Product ON Printer.model=Product.model WHERE Product.maker='B')
) result

正确的。

您的查询结果:

model               price

1121                850.0000

1750                1200.0000
于 2014-02-20T09:36:46.527 回答
0

演示 - http://www.sqlfiddle.com/#!6/e976b/2 - 重用由Mahmoud Gamal填充的输入数据

WITH CTE AS
(
SELECT model, price
from   PC 
UNION 
SELECT model, price
from   Printer 
UNION
SELECT model, price
from   Laptop 
)
SELECT C.model, C.price 
FROM   CTE as C INNER JOIN Product as P 
ON     C.model = P.model 
WHERE  P.maker = 'B';
于 2017-10-06T06:52:56.880 回答
-1
select pc.model,pc.price 
from product 
    inner join pc on pc.model=product.model and maker ='b' 
union 
select laptop.model,laptop.price 
from  product 
    inner join laptop on product.model=laptop.model and maker ='b' 
union 
select printer.model,printer.price 
from product 
    inner join printer on product.model=printer.model and maker ='b'
于 2014-05-15T10:44:02.990 回答