问题:两个表,第一个包含userid(约束fk)和列'name',第二个表包含两列int和id(外键),我需要的是找到max(column1-column2)和从第一个表中为其指定名称;
我在做什么: MySQL的>
select u.name, MAX(table2.column1-table2.column2) As var
from table1 u, table2 b
where u.userID(from table1) = b.userID(from table2)
and (b.column1-b.column2) = var;
在这种情况下它表示“未知列var”,是否可以使用withoud triggers / procedures?
any1?:)
答案 0 :(得分:0)
也许尝试子查询?
select u.name, (b.column1 - b.column2) as var
from table1 u, table2 b
where u.userID(from table1) = b.userID(from table2)
and (b.column1-b.column2) = ( select MAX(column1 - column2) from table2);
答案 1 :(得分:0)
我认为这是正确的解决方案:
select u.name,
MAX(table2.column1-table2.column2) As var
from table1 u
join table2 b
on b.userId = u.userId
where u.userID(from table1) = b.userID(from table2)
group by u.name