i tried this one但没有运气。我的要求是我有一个名为'tabAccount'的表。它有一些重复值和字段的空值 'parent_account'。我想只选择字段parent_account的不同值,也选择值为Ledger的字段group_or_ledger和值为Expense的字段root_type。我怎么能这样做?
我试过这样的事情
$expense = TabAccount::find()->select('DISTINCT `parent_account`')->where(['root_type'=>'Expense'])
->andWhere(['group_or_ledger'=>'Ledger'])
->andWhere(['not', ['parent_account' => null]])->all();
答案 0 :(得分:1)
distinct是一个布尔函数,请参阅Yii2 api distinct()
$query = TabAccount::find();
$query->select('parent_account');
$query->distinct();
$query->where(['root_type'=>'Expense']);
$query->andWhere(['group_or_ledger'=>'Ledger'])
$query->andWhere(['not', ['parent_account' => null]]);
$expense = $query->all();
您也可以尝试:
$query = new Query;
$query->select('parent_account')
->from(TabAccount::tableName())
->distinct()
->where(['root_type'=>'Expense'])
->andWhere(['group_or_ledger'=>'Ledger'])
->andWhere(['not', ['parent_account' => null]]);
$command = $query->createCommand();
$data = $command->queryAll();