0

i tried this one but with no luck. My requirement is that i have a table called 'tabAccount'. it has some duplicate values and null values for the field 'parent_account'. I want to select only the distinct values for the field parent_account, also field group_or_ledger with value Ledger and field root_type with value Expense . How can i do this?

i tried something like this

$expense    =   TabAccount::find()->select('DISTINCT `parent_account`')->where(['root_type'=>'Expense'])
                    ->andWhere(['group_or_ledger'=>'Ledger'])
                    ->andWhere(['not', ['parent_account' => null]])->all();
Community
  • 1
  • 1
Bloodhound
  • 2,720
  • 8
  • 31
  • 66

1 Answers1

1

distinct is a boolean function see 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();

You can also try:

$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();
BHoft
  • 1,593
  • 9
  • 18