Adam Adam - 7 months ago 15
SQL Question

MySQL Select within Select + using main WHERE Clause

I have the following MySQL Select:

gallery IS NOT NULL AND


You will see the "gallery IS NOT NULL AND" in the main WHERE Clause.

gallery is the AS from one of the selects above but in this format it errors.

Is there a way for me to make this select dependent on 'gallery' not being NULL?

Note: I tried to join to photos2 but is a one to many relationship so it brings back multiple rows - maybe a GROUP BY might help?

thanks
Adam

Answer

Try removing the gallery IS NOT NULL AND line and putting HAVING gallery IS NOT NULL before the ORDER clause.

Edit: Results from an aggregated function cannot be used in WHERE clauses, they have to be put in a HAVING clause.