我想像这样在mysql上使用选定的行。 我有一个关于价值的查询。
Select maxValue from tableOne
我想像这样在查询中使用此值。
Select (Select maxValue from tableOne where tableTwo.Aa=tableOne.Aa LIMIT 1) as maxValue,CASE tableTwo.Param WHEN 1 Then maxValue -50
When 2 Then maxValue-100
When 3 Then maxValue-400
When 4 Then maxValue-500
ELSE
maxValue
END ,
from tableTwo
我该如何使用?
答案 0 :(得分:1)
一种方法是
Select
CASE tableTwo.Param
WHEN 1 Then t.maxValue - 50
When 2 Then t.maxValue - 100
When 3 Then t.maxValue - 400
When 4 Then t.maxValue - 500
ELSE t.maxValue
END
from tableTwo
JOIN
(
SELECT
Aa
, MAX(maxValue)
FROM tableOne
GROUP BY Aa
) t
ON t.Aa = tableTow.Aa
答案 1 :(得分:0)
您只需要一个列,因此不需要加入,只需从具有该列的表中进行选择即可:
(Select maxValue from tableOne where tableTwo.Aa=tableOne.Aa LIMIT 1)
应该是
(Select maxValue from tableWithMaxValue LIMIT 1)
如果要联接表,则可以在外部查询中执行此操作,因此整个查询变为:
SELECT CASE tableTwo.Param WHEN 1 Then maxValue -50
WHEN 2 Then maxValue-100
WHEN 3 Then maxValue-400
WHEN 4 Then maxValue-500
ELSE maxValue END
FROM tableOne t1
JOIN tableTwo t2 ON t1.Aa = t2.Aa
LIMIT 1;