一直使用组合连接查询,师兄偏说要改为子查询的模式,好嘛,改,出现不少问题,记一下。
1.TABLE
子查询作为table的一部分,(ps:个人理解,可能不准确)即把子查询的结果作为一个新表可以和主表连接。这个很好理解,也很简单
直接上关键代码:
subQuery=
order_detail
->join(“a INNER JOIN {$this->pre}product_price
b ON a.price_id=b.price_id”)
->join(“INNER JOIN {$this->pre}sales_products
c ON b.sell_id=c.sell_id”)
->join(“INNER JOIN {$this->pre}product_pic
d ON b.pid =d.pid”)
->field(“a.order_number,a.item_id,d.s_100_pic,c.product_name,a.standard_model,c.maker_name,c.product_unit,
a.shop_qty,b.product_price”)
->group(“a.order_number”)
->where(
map2)−>buildSql();foreach(
list as
key=>
value){
whereorder["ordernumber"]=
list[
key][‘ordernumber′];
list[
key][‘detail′]=
order//可不关联order_list表
->table(
subQuery.”detail”)−>where(
where_order)//SUM(a.shop_qty) as total,可查不好绑
->field(“detail.item_id,detail.s_100_pic,detail.product_name,detail.standard_model,detail.maker_name,
detail.product_unit,detail.shop_qty,detail.product_price”)
->order(‘detail.item_id’)
->select();
2。IN
譬如我写的一个订单list和详情的问题
subQuery=
order_detail
->join(“a INNER JOIN {$this->pre}product_price
b ON a.price_id=b.price_id”)
->join(“INNER JOIN {$this->pre}sales_products
c ON b.sell_id=c.sell_id”)
->field(“a.order_id,a.shop_qty”)
->where(
map2)−>buildSql();//这也可以用select(flase)无关,看个人喜好上面的map2条件有部分是在详情信息里面的,如商品名称,价格等等,此时用连接查询就不能达到要求啦实现:嵌套着IN
count=$order
->join(“A INNER JOIN {$this->pre}hy_company
B ON A.uid=B.uid”)
->where(
map1)−>where(“A.orderidIN“.
subQuery)
->count();
总结一下:关键是区分这两种方法的差异,一不小心很容易弄错的。