Note #0: *
will get you all the columns, but it is recommended to write the required columns only. This may lead to better performance, as in most cases you do not require all the columns. Also, when table schema is changed (e.g. a column is added), there is chance that it is fetched uselessly.
Note #1: Yes, it can be removed. However, some programmers put it (especially when using dynamic queries) just to be able to have all conditions defined like this: AND {condition}
. This looks better, as all conditions are defined homogeneously and it should have no impact on performance.
Note #2: X OR X equals X, so it is recommended to remove it (DRY).
Note #3: The last condition can be defined without the parenthesis. Some programmers put parenthesis for conditions, just to be sure they do not face precedence problems. It is recommended to use parenthesis homogeneously (either use them for all conditions or no conditions). However, keep in mind that AND
has higher precedence over OR
and OR
conditions must use parenthesis when combined with AND ones.
About other aspects of the query:
FROM
and INNER JOIN
are usually put on separate lines for increased readability (all keywords like SELECT
, FROM
, JOIN
, WHERE
etc. tend to be on the left part of the query).
PHP variable interpolation - PHP has a nice feature that allows to integrate variables directly in the string to avoid concatenation (and lots of double quotes). So, instead of
"FROM " . $table . "posts INNER JOIN " . $table . "postmeta"
you can write
"FROM {$table}posts INNER JOIN {$table}postmeta"
or something similar.