I’m trying a subquery in MySQL using max(), and I keep running into an error. The gist of the query is below (though I’ve changed the field names).
select table1.field1, table1.field2, table2.field3, table2.field4, table3.field5,
(select max(age)
from age_table
where age_table.person = table2.person)
from table1
inner join table2 on table2.person = table1.person
inner join table3 on table3.person = table1.person
inner join age_table on age_table.person = table1.person
When I try this, I get a syntax error that points to
‘from age_table where age_table.person=table2.person’
…but I can’t figure out what the problem is.
Use table aliases to differentiate between tables, without having to use the full table name:
I removed what appeared to be a redundant JOIN to the AGE_TABLE, seeing as it wasn’t used in the SELECT clause.
It’s also good habit to define a column alias for derived column values – makes them easier to reference. See “max_age” for an example.