WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 8 months ago

#43630 new defect (bug)

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

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

Description

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

<?php
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:

<?php
SELECT DISTINCT meta_key
FROM $wpdb->postmeta
WHERE meta_key NOT BETWEEN '_' AND '_z'
  AND meta_key NOT LIKE %s
ORDER BY meta_key
LIMIT %d

It's work!

Attachments (1)

43630.diff (543 bytes) - added by markparnell 8 months ago.

Download all attachments as: .zip

Change History (3)

@markparnell
8 months ago

#1 @markparnell
8 months 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
8 months ago

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