数据查询

浏览:1341 发布日期:2014/04/16
3.2.1 - 致命 - 未处理
            $subQuery = $Info->field("$table1.info_id,
                                      $table1.name,
                                      $table1.id_card_no,
                                      $table1.tel,
                                      $table2.initial_id,
                                      group_concat(DISTINCT concat($table4.location_name,'〖',$table3.position,'〗') SEPARATOR '<br />') as group_location_name,
                                      group_concat(DISTINCT $table4.parent_id ORDER BY $table4.parent_id) as group_location_id,
                                      count(*) as count
                                    ")
                             ->join("INNER JOIN $table2 USING(info_id)")
                             ->join("INNER JOIN $table3 USING(initial_id)")
                             ->join("INNER JOIN $table4 USING(location_id)")
                             ->group("$table1.id_card_no")
                             ->order("count desc")
                             ->select(false);

            $subQuery2 = $Info->table($subQuery.' all_info')
                              ->where("group_location_id='".implode(',', $location)."'")
                              ->select(false);           
            
            $result = $Info->field("all_info_and_brand.*,
                                    group_concat(DISTINCT $table6.brand_name) as group_brand_name
                                  ")
                           ->table($subQuery2.' all_info_and_brand')
                           ->join("INNER JOIN $table5 ON $table5.info_id=all_info_and_brand.info_id")
                           ->join("INNER JOIN $table6 USING(brand_id)")
                           ->group("all_info_and_brand.id_card_no")
                           ->order("count desc")
                           ->select();

            echo $Info->getLastSql();
按照选择数据进行SQL搜索,如果上述的$location='1,2,3,4',则生成的SQL:SELECT all_info_and_brand.*,shop_info_brand.info_id,group_concat(DISTINCT shop_brand.brand_name) as group_brand_name
FROM ( SELECT * FROM ( 
            SELECT shop_info.info_id,shop_info.name,shop_info.id_card_no,shop_info.tel,shop_info_initial.initial_id,
            group_concat(DISTINCT concat(shop_location.location_name,'〖',shop_initial.position,'〗') SEPARATOR '') as 

group_location_name,
            group_concat(DISTINCT shop_location.parent_id ORDER BY shop_location.parent_id) as group_location_id,
            count(*) as count 
            FROM `shop_info` 
            INNER JOIN shop_info_initial USING(info_id) 
            INNER JOIN shop_initial USING(initial_id)
            INNER JOIN shop_location USING(location_id) 
            GROUP BY shop_info.id_card_no 
            ORDER BY count desc 
        )all_info
      WHERE ( group_location_id='1,`2`,`3`,4' ) ) 
    ) all_info_and_brand
INNER JOIN shop_info_brand on shop_info_brand.info_id=all_info_and_brand.info_id
INNER JOIN shop_brand on shop_brand.brand_id=shop_info_brand.brand_id
GROUP BY all_info_and_brand.info_id
ORDER BY count desc
上述的where字句中,group_location_id='1,`2`,`3`,4' 的2和3为什么会多出`符号,单独查询时正确,只要用子查询中出现错误。
评论(
后面还有条评论,点击查看>>