1

私は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 が製造するすべての製品 (種類は問いません) のモデルと価格を調べます。

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 に答える
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を参照してください。

次に、列ではなく別の行にデータを配置したい場合は、アンピボット関数を使用できます。

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 に答える
-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 に答える