Mysql查询多条件

时间:2013-10-07 18:56:53

标签: mysql sql

我试图像这样进行查询:

$query= "SELECT a.virtuemart_category_id, 
   a.virtuemart_product_id, 
   b.product_name, 
   b.product_desc, 
   c.product_price, 
   d.virtuemart_media_id, 
   e.file_url 
   FROM   wgny6_virtuemart_product_categories a 
   LEFT JOIN wgny6_virtuemart_products_en_gb b 
          ON a.virtuemart_product_id = b.virtuemart_product_id 
   LEFT JOIN wgny6_virtuemart_product_prices c 
          ON b.virtuemart_product_id = c.virtuemart_product_id 
   LEFT JOIN wgny6_virtuemart_product_medias d 
          ON b.virtuemart_product_id = d.virtuemart_product_id 
   LEFT JOIN wgny6_virtuemart_medias e 
          ON d.virtuemart_media_id = e.virtuemart_media_id 
   WHERE  b.product_name IS NOT NULL";

但我希望只有在a.virtuemart_category_id = $ id时才会发生这种情况。但我无法弄清楚我将如何添加这种条件。

2 个答案:

答案 0 :(得分:1)

SELECT ... 
FROM   ...
WHERE  b.product_name IS NOT NULL 
       AND a.virtuemart_category_id= $id // <-----------------------------------

如果您使用的是PHP,请确保使用带有预处理语句的绑定参数。

答案 1 :(得分:1)

简单地将额外条件放在条件

 AND a.virtuemart_category_id = $id 

在您的查询结束时。

<强>即

SELECT a.virtuemart_category_id,a.virtuemart_product_id,b.product_name,b.product_desc, c.product_price,d.virtuemart_media_id,e.file_url 
    FROM wgny6_virtuemart_product_categories a 
      LEFT JOIN wgny6_virtuemart_products_en_gb b ON a.virtuemart_product_id=b.virtuemart_product_id 
      LEFT JOIN  wgny6_virtuemart_product_prices c ON b.virtuemart_product_id=c.virtuemart_product_id 
      LEFT JOIN wgny6_virtuemart_product_medias d ON b.virtuemart_product_id=d.virtuemart_product_id 
      LEFT JOIN wgny6_virtuemart_medias e ON d.virtuemart_media_id=e.virtuemart_media_id 
  WHERE b.product_name IS NOT NULL AND a.virtuemart_category_id = $id