Yii2:如何编写不同的 SQL 查询?

2022-01-07 00:00:00 php mysql yii2

我想在 Yii 2 中实现以下 SQL 查询,但没有成功.

I want to implement following SQL queries in Yii 2 but with no success.

这应该给出唯一公司名称的总数:

This should give total number of unique company names:

SELECT count(DISTINCT(company_name)) FROM clients

这应该显示带有 client codeid(PK)company_name :

And this should display company_name with client code and id(PK):

SELECT (DISTINCT(company_name,client_code)) FROM clients

如何实现这一目标?

推荐答案

回答我自己的问题,我得到了以下可行的解决方案:

Answering my own question I got following working solutions:

获得唯一company_name的计数:

Got the count for unique company_name:

$my = (new yiidbQuery())
    ->select(['company_name',])
    ->from('create_client')
    ->distinct()
    ->count();
echo $my;

不同company_nameclient_code 的列表:

List of distinct company_name and client_code:

$query = new yiidbQuery();
$data = $query->select(['company_name','client_code'])
    ->from('create_client')
    ->distinct()
    ->all();
if ($data) {
    foreach ($data as $row) {
        echo 'company_name: ' . $row['company_name'] . ' client_code: ' . $row['client_code'] . '<br>';
    }
}

相关文章