Make WordPress Core

Opened 6 years ago

Last modified 4 years ago

#43630 new defect (bug)

HAVING without GROUP BY in /wp-admin/includes/template.php

Reported by: vsvlad's profile vsvlad Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: Administration Keywords: has-patch
Focuses: template Cc:


I found in file /wp-admin/includes/template.php, that used HAVING without GROUP BY. It's syntax correct for MySQL

614   $sql = "SELECT DISTINCT meta_key
615                             FROM $wpdb->postmeta
616                             WHERE meta_key NOT BETWEEN '_' AND '_z'
617                             HAVING meta_key NOT LIKE %s
618                             ORDER BY meta_key
619                             LIMIT %d";

Official MySQL doc say:
"HAVING is merged with WHERE if you do not use GROUP BY or aggregate functions (COUNT(), MIN(), and so on).

But this incorrect for other databases. Such as SQLite say it's wrong, when using WP with it.

Maybe in this case, need replace 'HAVING' on 'AND'? After replace like:

FROM $wpdb->postmeta
WHERE meta_key NOT BETWEEN '_' AND '_z'
  AND meta_key NOT LIKE %s
ORDER BY meta_key

It's work!

Attachments (1)

43630.diff (543 bytes) - added by markparnell 4 years ago.

Download all attachments as: .zip

Change History (3)

4 years ago

#1 @markparnell
4 years ago

  • Keywords has-patch needs-testing added

Patch replaces HAVING with AND as suggested. My own testing supports @vsvlad's assertion that the updated query returns the same results.

I had a look through the codebase and couldn't find any other instances of a query containing HAVING without GROUP BY. This appears to be the only case.

#2 @markparnell
4 years ago

  • Keywords needs-testing removed
Note: See TracTickets for help on using tickets.