2012-01-06 93 views
1

我需要添加另一個AND條件,但從另一個表是否可以添加條件?額外的mysql選擇查詢條件?

目前我的MySQL查詢是:

$category_min_price = db_get_field("SELECT min(price) FROM ?:product_prices p, ?:products_categories c WHERE p.product_id=c.product_id AND c.category_id = ?i",$category_id); 

,但我需要檢查,如果該產品是在位於另一個表上述聲明活躍,詳情如下:

table: ?:products

col: status

如果是單獨將FROM ?:products WHERE product_id = "ID" AND status = "A"

有誰知道我可以歸併到上面的查詢,如此刻它得到的最低報價,但得到的最小价格甚至禁用產品,讓需要補充的狀態條件中只能做積極的產品

非常感謝先進!

回答

5

不要加入,我的好先生。

select 
    min(pp.price) 
from 
    products p 
    inner join product_prices pp on 
     p.product_id = pp.product_id 
    inner join products_categories pc on 
     p.product_id = pc.product_id 
where 
    pc.category_id = ?i 
    and p.status = 'A' 

你在那裏有交叉連接。雖然你可以把它們當作內部連接(或者任何連接),但是不清楚什麼是連接到什麼內容。

+0

超級!工作了一種享受,非常感謝! – 2012-01-06 19:47:03