当前位置: 代码迷 >> 综合 >> 07thinkphp 学习 综合-未完结
  详细解决方案

07thinkphp 学习 综合-未完结

热度:105   发布时间:2023-10-10 23:34:50.0

 1.column 获取 brand_id字段的列表

$brand_ids = Db::table('tb_category_brand')->where('category_id',$cid)->column('brand_id');#dump($brand_ids);
2.field 获取指定字段的值 id,name
$brands = Db::table('tb_brand')->field('id,name')->whereIn('id',$brand_ids)->select();

3.value 查询单条记录

$brand_ids = Db::table('tb_category_brand')->where('category_id',$cid)->value('brand_id');

4.闭包问题 --- 子查询

[ info ] [ DB ] INIT mysql
[ sql ] [ DB ] CONNECT:[ UseTime:0.002354s ] mysql:host=127.0.0.1;dbname=heima;charset=utf8
[ sql ] [ SQL ] SHOW COLUMNS FROM `tb_brand` [ RunTime:0.001496s ]
[ sql ] [ SQL ] SHOW COLUMNS FROM `tb_category_brand` [ RunTime:0.004418s ]
[ sql ] [ SQL ] SELECT `id`,`name` FROM `tb_brand` WHERE  `id` IN (SELECT `brand_id` FROM `tb_category_brand` WHERE  `category_id` = 76) [ RunTime:0.002512s ]
$brands = Db::table('tb_brand')->field('id,name')->whereIn('id',function ($query) use ($cid) {#闭包内不可以用外部变量  所以需要use 引入#闭包返回一个sql$query->table('tb_category_brand')->where('category_id',$cid)->field('brand_id');})->select();