问题
I'm working on a project and need to create a few join statements. I tried to make one that selects the maximum value of a column in three different tables. I'm getting an error on the SELECT that says "select is not valid at this server position, expecting FOR, LOCK, TABLE, VALUES, WITH, '('". Not sure what to do, any advice? Code is below.
SELECT MAX(cn.TotalDollarPerArea) as 'North Total Dollar per Area',
MAX(cs.TotalDollarPerArea) as 'South Total Dollar per Area',
MAX(ncl.TotalDollarPerArea) as 'Non-Climate Total Dollar Per Area',
FROM ClimateNorth cn,
JOIN ClimateSouth cs using (TotalDollarPerArea),
JOIN NonClimate ncl using (TotalDollarPerArea);
回答1:
Just use subqueries:
SELECT (SELECT MAX(cn.TotalDollarPerArea)
FROM ClimateNorth cn
) as `North Total Dollar per Area`,
(SELECT MAX(cs.TotalDollarPerArea)
FROM ClimateSouth cs
) as `South Total Dollar per Area`,
(SELECT MAX(ncl.TotalDollarPerArea)
FROM NonClimate ncl
) as `Non-Climate Total Dollar Per Area`;
Note: Only use single quotes for string and date constants. There are better escape characters for column aliases.
来源:https://stackoverflow.com/questions/65172090/need-help-joining-multiple-tables-to-give-the-maximum-number-for-each-one