Adam Adam - 2 years ago 105
SQL Question

MySQL Select within Select + using main WHERE Clause

I have the following MySQL Select:


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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download