SQL查询ORACLE选择从最贵到最便宜排序的所有设备

72qzrwbm  于 2023-03-01  发布在  Oracle
关注(0)|答案(1)|浏览(174)

嗨,我有这个数据库结构db structure,我需要选择从最昂贵的到最便宜的订购的所有设备。输出应该有序列号,制造商,价格,类型,但制造商属性是在产品表,我不知道如何添加到查询。属性与所有表共同是型号。

table Laptop (serial_number varchar(50),
 model,
 price,
 speed,
 ram,
 hd,
 screen,
 CONSTRAINT laptop_pk PRIMARY KEY (serial_number),
 CONSTRAINT laptop_2_product_fk
 FOREIGN KEY (model)
 REFERENCES Product(model)); 

table Product (model varchar(50),
 maker,
 type,
 CONSTRAINT product_pk PRIMARY KEY (model));

table Printer (serial_number varchar(50),
 model,
 price,
 color,
 type,
 CONSTRAINT printer_pk PRIMARY KEY (serial_number),
 CONSTRAINT printer_2_product_fk
 FOREIGN KEY (model)
 REFERENCES Product(model));

table PC (serial_number varchar(50),
 model,
 price,
 speed,
 ram,
 hd,
 cd,
 CONSTRAINT pc_pk PRIMARY KEY (serial_number),
 CONSTRAINT pc_2_product_fk
 FOREIGN KEY (model)
 REFERENCES Product(model));

table Storage (storage_id varchar(50),
 model,
 serial_number,
 transfered_to);

我尝试这个,但当然这只是给予我的模型,我需要序列号,制造商,价格和类型。我尝试在livesql上。

WITH MAX
AS (
    SELECT model, price FROM PC
    UNION 
    SELECT model, price FROM Laptop
    UNION 
    SELECT model, price FROM printer
  
)

SELECT model FROM MAX
WHERE price = (
    SELECT MAX(price) 
    FROM MAX
)
ar7v8xwq

ar7v8xwq1#

必须连接所有表并选择所需列

WITH MAX
AS (
    SELECT PC.model,st.serial_number, pr.maker, p.rtype, PC.price 
  FROM PC INNER JOIN Product pr ON PC.model = pr.model
  INNER JOIN  Storage st ON PC.model = st.model
    UNION 
    SELECT Laptop.model ,st.serial_number, pr.maker, p.rtype, Laptop.price 
  FROM Laptop INNER JOIN Product pr ON Laptop.model = pr.model
  INNER JOIN  Storage st ON Laptop.model = st.model 
    UNION 
    SELECT printer.model,st.serial_number, pr.maker, p.rtype, printer.price 
  FROM printer INNER JOIN Product pr ON Laptop.model = pr.model
  INNER JOIN  Storage st ON printer.model = st.model 
  
)

SELECT model,serial_number, maker, rtype, price 
FROM MAX
WHERE price = (
    SELECT MAX(price) 
    FROM MAX
)
ORDER BY price

相关问题