我想知道是否可以将参数传递给select子查询。
我想要做的是从一张桌子收集一些产品数据,然后将物品的重量交叉到运输表中的运输成本以返回成本。
类似的东西:
select cataloguenumber, productname,
(select shipping.carriagecost
from shipping
where shipping.carriageweight = weight) as carriagecost
from products
此致
DPERROTT
答案 0 :(得分:7)
虽然子查询可以工作,但更好,更易读和有效的方法来定义它将如下:
SELECT p.cataloguenumber
, p.productname,
, s.carriagecost
FROM products p
INNER JOIN
shipping s
ON p.weight = s.carriageweight
这假定所有产品重量在装运表中都有相应的条目。如果情况并非如此,则从INNER JOIN
更改为LEFT JOIN
并处理任何空值。
答案 1 :(得分:4)
select cataloguenumber, productname, shipping.carriagecost as carriagecost
from products, shipping
where shipping.carriageweight = products.weight
或者我错过了什么?
答案 2 :(得分:1)
SELECT DISTINCT cataloguenumber, productname, shipping.carriagecost
FROM products
LEFT OUTER JOIN shipping
ON shipping.carriageweight = products.weight
答案 3 :(得分:0)
您的子查询应该只返回1行,如果它返回的更多,则查询将在运行时抛出错误。
答案 4 :(得分:0)
我认为这是可能的,但是您应该检索要在父查询中传递的列。
select cataloguenumber, productname, weight
(select shipping.carriagecost
from shipping
where shipping.carriageweight = weight) as carriagecost
from products
答案 5 :(得分:0)
SELECT DISTINCT products.cataloguenumber, products.productname, shipping.carriagecost
FROM products
LEFT JOIN shipping ON shipping.carriageweight = products.weight