yii2 Query Builder 查询打印sql语句

Posted 不知起什么名字

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii2 Query Builder 查询打印sql语句相关的知识,希望对你有一定的参考价值。

$query = new Query();
$query->select(‘gs.*, g.goods_images, sa.attr_name, sa.is_default, sa.alias_sort‘)->from(GoodsSpec::tableName() . ‘ gs‘);
$query->leftJoin(SpecAlias::tableName() . ‘ sa‘, ‘gs.attr_id = sa.attr_id and gs.goods_id = sa.goods_id‘);
// $query->leftJoin(CatSpec::tableName() . ‘ c‘, ‘gs.attr_id = c.attr_id and gs.cat_id = c.cat_id‘);
$query->leftJoin(AttrValue::tableName() . ‘ av‘, ‘gs.attr_vid = av.attr_vid‘);
$query->leftJoin(Goods::tableName() . ‘ g‘, ‘g.goods_id = gs.goods_id‘);
$query->andWhere(‘gs.goods_id in (‘.$goods_ids.‘)‘);
// $query->groupBy([‘gs.goods_id‘]);

$query->orderBy([
‘sa.is_default‘ => SORT_DESC,
‘gs.spec_sort‘ => SORT_ASC,
‘av.attr_vsort‘ => SORT_ASC
]);
$sql = clone $query;
var_dump($sql->createCommand()->getRawSql());

以上是关于yii2 Query Builder 查询打印sql语句的主要内容,如果未能解决你的问题,请参考以下文章

Yii2中indexBy()的使用

Laravel 查询错误 - 调用未定义的方法 Illuminate\Database\Query\Builder::query()

jquery query builder只适合单表查询吗

react-query-builder查询构建器中文文档

Laravel:在“query-builder”或“eloquent”中更改原始查询

Yii2-Restful-Query