2012-01-06 100 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