mySQL加入哪里?

时间:2011-12-09 11:36:55

标签: php mysql database join where

是否可以将WHERE添加到mysql JOIN语句中,例如:

$result = mysql_query("SELECT * FROM site_products JOIN site_trans ON site_products.product_count = site_trans.trans_inventory WHERE site_products.product_id = site_trans.trans_product");

这可能吗?

3 个答案:

答案 0 :(得分:8)

是的,这是可能的,但您的查询可能无法提供所需的结果:

SELECT * FROM site_products 
JOIN site_trans ON site_products.product_count = site_trans.trans_inventory 
WHERE site_products.product_id = site_trans.trans_product

相反,您应该将其写为附加的JOIN条件,如此

SELECT * FROM site_products 
JOIN site_trans ON site_trans.trans_inventory = site_products.product_count
     AND site_trans.trans_product = site_products.product_id

您还可以添加WHERE子句

SELECT * FROM site_products 
JOIN site_trans ON site_trans.trans_inventory = site_products.product_count
     AND site_trans.trans_product = site_products.product_id
WHERE site_products.product_id = 2

答案 1 :(得分:0)

SELECT * FROM site_products JOIN site_trans ON (site_products.product_count = site_trans.trans_inventory AND site_products.product_id = site_trans.trans_product)
WHERE 1

答案 2 :(得分:0)

是的,这可以在MYSQL连接查询中的条件中使用。即使你可以在使用AND运算符的情况下使用多个条件。