如何使用一个mysql查询从多个表中检索数据

时间:2013-11-22 15:46:08

标签: mysql join virtuemart

我尝试编写一个查询,以便在virtmart 2中检索我们产品的完整信息。

首先,我尝试使用单个INNER JOIN,如下面的示例:

    $query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`

    FROM `pmqbw_virtuemart_products_el_gr` AS p 
    INNER JOIN `pmqbw_virtuemart_product_categories` ON p.`virtuemart_product_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_product_id`
    LIMIT 0 ,10"; 

以上效果很好!

然后我试着变得更复杂,我写了以下内容:

    $query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`,    `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`, `pmqbw_virtuemart_categories_el_gr`.`category_name`

    FROM `pmqbw_virtuemart_products_el_gr` AS p

    INNER JOIN p 
ON `pmqbw_virtuemart_product_categories`.`virtuemart_product_id`=p.`virtuemart_product_id`


    INNER JOIN `pmqbw_virtuemart_categories_el_gr` AS x
ON x.`virtuemart_category_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_category_id`

LIMIT 0 ,10"; 

但这里出了点问题,我无法理解。

之后我将尝试检索更多表和更多联接,以便提供我需要的产品数据。

提前感谢您的帮助!


实际上我想从下表中检索数据。 来自pmqbw_virtuemart_products_el_gr I want fields: product_name, product_s_desc, virtuemart_product_id`

来自pmqbw_virtuemart_product_categories我想要字段:virtuemart_product_id,virtuemart_category_id

来自pmqbw_virtuemart_categories_el_gr我想要字段:virtuemart_category_id,category_name

来自pmqbw_virtuemart_product_medias我想要字段:virtuemart_product_id,virtuemart_media_id

pmqbw_virtuemart_medias我想要字段:virtuemart_media_id,file_url,file_url_thumb其中file_mimetype = image / jpeg AND file_type = product

我尝试使用左连接进行不同的查询,但不能正常工作。

    $sql = "SELECT N.product_name,I.virtuemart_media_id,M.virtuemart_product_id,MN.category_name 

FROM pmqbw_virtuemart_categories_el_gr AS M 

LEFT JOIN pmqbw_virtuemart_products AS P ON P.virtuemart_product_id = M.virtuemart_product_id

LEFT JOIN pmqbw_virtuemart_products_el_gr  AS N ON N.virtuemart_product_id = M.virtuemart_product_id

LEFT JOIN pmqbw_virtuemart_product_medias  AS I ON I.virtuemart_product_id = M.virtuemart_product_id

LEFT JOIN pmqbw_virtuemart_product_categories AS MN ON MN.virtuemart_category_id = M.virtuemart_category_id

WHERE M.virtuemart_category_id = 1";

任何帮助将不胜感激!

谢谢!

谢谢大家的帮助!

运行正常的查询如下:

    $sql = "SELECT DISTINCT X.virtuemart_product_id, X.product_name, M.category_name, Y.virtuemart_product_id, W.file_url

    FROM pmqbw_virtuemart_products_el_gr AS X
    INNER JOIN pmqbw_virtuemart_product_categories AS P ON P.virtuemart_product_id = X.virtuemart_product_id
    AND P.virtuemart_category_id =2

    INNER JOIN pmqbw_virtuemart_categories_el_gr AS M ON M.virtuemart_category_id = P.virtuemart_category_id"

    INNER JOIN pmqbw_virtuemart_product_medias AS Y ON Y.virtuemart_product_id = X.virtuemart_product_id

    INNER JOIN pmqbw_virtuemart_medias AS W ON W.virtuemart_media_id = Y.virtuemart_media_id;

萌芽的是,每个产品都有两个或更多的图像,我只想要一个。 我必须考虑一些事情才能消除它! 如果有人在他的脑海中寻求帮助,那将非常受欢迎!

谢谢!

好的,我找到了GROUP BY Y.virtuemart_product_id的解决方案 :)

谢谢大家!

0 个答案:

没有答案