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

时间:2013-03-25 09:48:15

标签: sql sql-server tsql

我有4张桌子

PC

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生产的所有产品(任何类型)的型号和价格。

7 个答案:

答案 0 :(得分:2)

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'

答案 1 :(得分: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'

答案 2 :(得分: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 Fiddle Demo

这会给你:

| MODEL | PRICE |   TYPE |
--------------------------
|  1121 |   850 |     PC |
|  1121 |   850 |     PC |
|  1121 |   850 |     PC |
|  1750 |  1200 | Laptop |

答案 3 :(得分: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 with Demo

答案 4 :(得分: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

答案 5 :(得分: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';

答案 6 :(得分:-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'