使用联合查询从数据库检索数据

时间:2015-10-15 10:14:11

标签: mysql

我想使用union查询检索数据以获得与其他表product_id相等的product_id。在我使用union查询加入两个表之后,我想加入两个表的product_id。我不能尝试。请帮帮我。

$query_filter=mysql_query("SELECT  (select ttdbrand.name) as brand,
                                       (select cproduct_brand.product_id) as product_brand
                               FROM `taxonomy_term_data` as ttdbrand
                               INNER JOIN
                                    `node` as n
                               INNER JOIN       
                                    `field_data_field_brand` as fbrand
                               INNER JOIN
                                    `commerce_product` as cproduct_brand
                               INNER JOIN
                                    `field_data_field_product` as fproduct_brand

                               ON n.nid=fbrand.entity_id
                               AND ttdbrand.tid=fbrand.field_brand_tid  
                               AND fproduct_brand.entity_id=n.nid                                   
                               and  cproduct_city.product_id=cproduct_brand.product_id
                               HAVING brand in ('$data_brand')

                               UNION ALL

                               SELECT (select ttdcity.name ) as city,
                                      (select cproduct_city.product_id ) as product
                               FROM `taxonomy_term_data` as ttdcity
                               INNER JOIN
                                  `node` as n
                               INNER JOIN       
                                `field_data_field_city` as fcity
                               INNER JOIN
                                    `commerce_product` as cproduct_city
                               INNER JOIN
                                    `field_data_field_product` as fproduct_city                                     
                               ON n.nid=fcity.entity_id
                               AND ttdcity.tid=fcity.field_city_tid 
                               AND fproduct_city.entity_id=n.nid
                               AND cproduct_city.product_id=fproduct_city.field_product_product_id                                                             
                               HAVING city IN ('$data_city')                               
                               order by product_brand DESC

                               ") or die(mysql_error());


    while($row = mysql_fetch_assoc($query_filter)) {            
        $row_array[]=$row;                 
    }

0 个答案:

没有答案