MYSQL GROUP By + ORDER BY FIELD()

时间:2013-06-12 09:09:36

标签: mysql sql-order-by

有树表:

Emails_blocks

blocks_id  blocks_pos  url_type  blocks_folder  
1          1           1         emails_block   
2          2           1         emails_show    
3          3           1         emails_add     
4          4           0         emails_share   

Emails_blocks_text

texts_id  blocks_id  langs_code  
2         1          ru          
1         1          ua          
3         2          ua          

Emails_blocks_aliases

blocks_id  aliases_name  
1          news          
2          drafts        

并有查询:

SELECT `blocks_id`,  `blocks_folder`, `blocks_name`, `aliases_name`, `langs_code`
FROM `a_emails_blocks`
LEFT JOIN `a_emails_blocks_text` USING(`blocks_id`)
LEFT JOIN `a_emails_blocks_aliases` USING(`blocks_id`)
GROUP BY `blocks_id`
ORDER BY FIELD(`a_emails_blocks_text`.`langs_code`, 'ua', 'ru')

但结果我在blocks_id = 1 而不是langs_code'ua',我有langs代码'ru'。 我不明白为什么不从餐桌上订购。

blocks_id  blocks_folder  aliases_name  langs_code 

1          emails_block   news          ru          
2          emails_show    drafts        ua          
3          emails_add                               
4          emails_share                             

1 个答案:

答案 0 :(得分:0)

您应该SELECT只有GROUP BY中的字段或汇总结果

实际上在你的情况下你不需要GROUP BY,因为你没有任何聚合函数(总和,计数,平均等)