MySQL-在第二个动态列中引用动态列

MySQL-在第二个动态列中引用动态列,mysql,Mysql,我有一张这样的桌子: user | test_score a | 50 b | 60 我需要一个查询结果,它的列是测试分数的1.5倍,然后是与原始结果的差值。结果将是: user | test_score | adjusted_test_score | test_score_diff a | 50 | 75 | 25 b | 60 | 90 | 30 我尝试

我有一张这样的桌子:

user | test_score
  a  |  50
  b  |  60
我需要一个查询结果,它的列是测试分数的1.5倍,然后是与原始结果的差值。结果将是:

user | test_score | adjusted_test_score | test_score_diff
  a  |  50        |            75       |   25
  b  |  60        |            90       |   30
我尝试的查询:

 SELECT
 user AS user,
 test_score AS test_score,
 (test_score * 1.5) AS adjusted_test_score,
 (adjusted_test_score - test_score) AS test_score_diff
返回一个错误,“调整后的测试分数”列不存在

有没有不进行联接就引用此列的方法?

试试:

SELECT user, test_score, adjusted_test_score,
    (adjusted_test_score - test_score) AS test_score_diff
FROM (
    SELECT user, test_score, (test_score * 1.5) AS adjusted_test_score
    FROM source_table
)
或者您也可以这样做:

SELECT user, test_score, (test_score * 1.5) AS adjusted_test_score,
    ((test_score * 1.5) - test_score) AS test_score_diff
FROM source_table

为什么不重复一遍呢

挑选 用户作为用户, 测试分数作为测试分数, (测试分数*1.5)作为调整后的测试分数,
((测试分数*1.5)-测试分数)作为测试分数差异

我使用第二种方法作为解决方法。第一种方法是我没有想到的。你不能像我尝试的那样引用列别名,这似乎很奇怪。谢谢