简体   繁体   中英

Can't sort query results in ascending order

I'm trying to sort the following select query in ascending order by Meds_Name, but can't figure out what I'm doing wrong. It's instead sorting all the names that start with lower case letters first, and then following up with those that start with upper case.

Query works when I interpret it and execute in MySQL, but just not in Yii. Can anyone see where I'm going wrong:

$specs = Yii::app()->db->createCommand()
                ->select("im.*,m.name as Meds_Name,mf.name as Meds_Freq_name,
                            mr.name as Meds_Route_name,mu.name as Meds_Unit_name,
                            CONCAT_WS('/',im.Meds_StartMM,im.Meds_StartYYYY) as Meds_StartDate,
                            CONCAT_WS('/',im.Meds_EndMM,im.Meds_EndYYYY) as Meds_EndDate")
                ->from('indiv_meds im')
                ->leftJoin('meds m', 'im.Meds_Name=m.id')
                ->leftJoin('med_freq mf', 'im.Meds_Freq=mf.id')
                ->leftJoin('med_route mr', 'im.Meds_Route=mr.id')
                ->leftJoin('lkpmeds mu', 'im.Meds_Unit=mu.id')
                ->where($select_condition_declare)
                ->limit($limit, $start)
                ->order(array('Meds_Name asc'))
                ->queryAll();

This is simply because the result sorting is based on the ASCII code. You can solve the problem by converting your query result sorting column to lowercase/uppercase:

$specs = Yii::app()->db->createCommand()
                ->select("im.*, LOWER(m.name) as Meds_Name,mf.name as Meds_Freq_name,
                            mr.name as Meds_Route_name,mu.name as Meds_Unit_name,
                            CONCAT_WS('/',im.Meds_StartMM,im.Meds_StartYYYY) as Meds_StartDate,
                            CONCAT_WS('/',im.Meds_EndMM,im.Meds_EndYYYY) as Meds_EndDate")
                ->from('indiv_meds im')
                ->leftJoin('meds m', 'im.Meds_Name=m.id')
                ->leftJoin('med_freq mf', 'im.Meds_Freq=mf.id')
                ->leftJoin('med_route mr', 'im.Meds_Route=mr.id')
                ->leftJoin('lkpmeds mu', 'im.Meds_Unit=mu.id')
                ->where($select_condition_declare)
                ->limit($limit, $start)
                ->order(array('Meds_Name asc'))
                ->queryAll();

Well I don't know much about Yii but here is what I think may help you:

->order(array('m.name asc'))
->limit($limit, $start)

"LIMIT" always comes after "ORDER BY"


Update:

Ok got it :)

@sharcfinz I was looking here and at this URL http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html they use this way

new \yii\db\Query()

for the Query Builder and I think you just mixed a little bit the Query Builder commands with the direct SQL Query command

Yii::app()->db->createCommand()

you are using.

The "createCommand()" its used when you are going to insert an dentire SQL query command directly like

Yii::app()->db->createCommand("SELECT id, name FROM tableA")->queryAll();

but to use the "->select()","->order()", etc you have to use this way

$rows = (new \yii\db\Query())
    ->select(['id', 'email'])
    ->from('user')
    ->where(['last_name' => 'Smith'])
    ->limit(10)
    ->all();

将订单更改为

->order('m.Meds_Name asc')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM