tp5 子查询统计另一张表的长度

2019-01-10  本文已影响0人  一个人_f993

子查询统计另一张表的长度


$subQuery= Db::table('adv')->alias('adv')

->field('count(adv.ap_id)')

->where('adv.ap_id=adp.ap_id')

->buildSql();

$list= Db::table('adv_position')->alias('adp')

->field("adp.*,$subQuery as img_count")

->where($condition)

->select();


生成的sql语句
SELECT

adp.ap_id, (

SELECT

count(adv.ap_id)

FROM

adv AS adv

WHERE

adv.ap_id = adp.ap_id

) AS img_count

FROM

adv_position AS adp;


上一篇下一篇

猜你喜欢

热点阅读