Thinkphp子查询问题总结

一直使用组合连接查询,师兄偏说要改为子查询的模式,好嘛,改,出现不少问题,记一下。
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();//selectflase)map2IN count=$order
->join(“A INNER JOIN {$this->pre}hy_company B ON A.uid=B.uid”)
->where( map1)>where(A.orderidIN. subQuery)
->count();

总结一下:关键是区分这两种方法的差异,一不小心很容易弄错的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值