2010-10-11 157 views
0

我想知道是否可以將參數傳遞給select子查詢。將參數傳遞給子查詢

我想要做的是從一個表中收集一些產品數據,然後在裝運表中將物品的重量交叉給它的運輸成本以返回成本。

類似:

select cataloguenumber, productname, 
     (select shipping.carriagecost 
     from shipping 
     where shipping.carriageweight = weight) as carriagecost 
from products 

問候

DPERROTT

回答

3
select cataloguenumber, productname, shipping.carriagecost as carriagecost 
from products, shipping 
where shipping.carriageweight = products.weight 

還是我錯過了什麼?

0

你的子查詢應該只返回1行,如果它返回更多的則是你的查詢將扔在運行時錯誤。

+0

嗨Numenor,查詢只會檢索一個值作爲馬車會有一個具體的價值。即重量1 =運輸4.99 – DPERROTT 2010-10-11 11:15:40

+0

我也同意加入方法,因爲它會運行得更快,然後子查詢的情況下表中有許多行 – Numenor 2010-10-11 11:29:40

1
SELECT DISTINCT cataloguenumber, productname, shipping.carriagecost 
FROM products 
LEFT OUTER JOIN shipping 
ON shipping.carriageweight = products.weight 
0

這是可能的,我認爲,但你應該檢索你想傳遞給你的父查詢的列。

select cataloguenumber, productname, weight 
     (select shipping.carriagecost 
     from shipping 
     where shipping.carriageweight = weight) as carriagecost 
from products 
7

雖然子查詢會的工作,更好的,更具可讀性和有效的方式來定義,這將是如下:

SELECT p.cataloguenumber 
,  p.productname, 
,  s.carriagecost  
FROM products p 
    INNER JOIN 
     shipping s 
    ON p.weight = s.carriageweight 

這假定所有產品的重量在航運表中的相應條目。如果情況並非如此,則從INNER JOIN更改爲LEFT JOIN並處理任何空值。

0
SELECT DISTINCT products.cataloguenumber, products.productname, shipping.carriagecost 
FROM products 
LEFT JOIN shipping ON shipping.carriageweight = products.weight