Group by,Order by在MySQL中工作,但不在MariaDB中

时间:2018-11-20 01:04:23

标签: wordpress mariadb

我们设法回过头来整理一些非常复杂的(对我们而言)数据库查询,这些查询将一些动态meta_values与WordPress中的常规Post信息结合在一起,以便得到漂亮的完整key => value对列表。它运行了好几年,但是现在我们改用了MariaDB,现在订购不正确了,我不确定为什么。有关更多格式设置,请参见以下https://screencast.com/t/mFILYpWf上的一个大而长的示例或屏幕截图。

关于如何首先按menu_order,last_name然后first_name进行订购的任何想法?

一如既往的感谢!

SELECT *
FROM
(
    SELECT wp_posts.ID, wp_posts.menu_order, wp_posts.post_title,
        wp_posts.post_name,
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_job_title'
                 THEN wp_postmeta.meta_value END ) AS 'job_title',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_prefix'
                 THEN wp_postmeta.meta_value END ) AS 'prefix',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_first_name'
                 THEN wp_postmeta.meta_value END ) AS 'first_name',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_middle_name'
                 THEN wp_postmeta.meta_value END ) AS 'middle_name',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_last_name'
                 THEN wp_postmeta.meta_value END ) AS 'last_name',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_suffix'
                 THEN wp_postmeta.meta_value END ) AS 'suffix',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_languages'
                 THEN wp_postmeta.meta_value END ) AS 'languages',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_email'
                 THEN wp_postmeta.meta_value END ) AS 'email',
        MAX(CASE WHEN wp_postmeta.meta_key = 'lawyer_phone'
                 THEN wp_postmeta.meta_value END ) AS 'phone',
        MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_offices' THEN wp_postmeta.meta_value END ) AS 'offices',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_linkedin' THEN wp_postmeta.meta_value END ) AS 'linkedin',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_biography' THEN wp_postmeta.meta_value END ) AS 'biography',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_bio_field_credentials' THEN wp_postmeta.meta_value END ) AS 'bio_field_credentials',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_bio_field_affiliations' THEN wp_postmeta.meta_value END ) AS 'bio_field_affiliations',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_bio_field_accolades' THEN wp_postmeta.meta_value END ) AS 'bio_field_accolades',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_bio_field_notable_decisions' THEN wp_postmeta.meta_value END ) AS 'bio_field_notable_decisions',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_bio_field_education_admissions' THEN wp_postmeta.meta_value END ) AS 'bio_field_education_admissions',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_bio_image' THEN wp_postmeta.meta_value END ) AS 'bio_image',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_directory_image' THEN wp_postmeta.meta_value END ) AS 'directory_image',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_pdf_image' THEN wp_postmeta.meta_value END ) AS 'pdf_image',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_practices' THEN wp_postmeta.meta_value END ) AS 'practices',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_related_posts' THEN wp_postmeta.meta_value END ) AS 'related_posts',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_other_news' THEN wp_postmeta.meta_value END ) AS 'other_news',MAX( CASE WHEN wp_postmeta.meta_key = 'lawyer_related_staff' THEN wp_postmeta.meta_value END ) AS 'related_staff', concat( 'http://wiedner.localhost/lawyer/', wp_posts.post_name, '/' ) AS 'permalink' FROM wp_posts LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'lawyer'
    GROUP BY wp_posts.ID
    ORDER BY menu_order asc, last_name asc, first_name asc
) AS t
WHERE 1=1

2 个答案:

答案 0 :(得分:1)

通过摆脱外面的SELECT来简化它!

原因是“派生”表(您的子查询)没有顺序。 (这在标准SQL中已经存在很长时间了,但是直到最近才使用了MariaDB和后来的MySQL来利用它。)也就是说,执行该代码的目的是摆脱您精心编写的ORDER BY

但是,通过内部查询停止;必须遵守ORDER BY

答案 1 :(得分:0)

MariaDB可能具有不同的模式集。试试看,看看here

 SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',));

也可能是重复的,请参见此related question.