我想基本上取一个数字(total_score)并除以< =今天的(release_date)数。我肯定想在SELECT语句中进行数学运算。我尝试了这个但它似乎不起作用:
SELECT
total_score / COUNT(release_date <= CURDATE()) as final_score
from movies
WHERE id = 12
所以说总得分是400,而且&lt; =今天的release_dates数量是2. final_score应该是200.
我可以像这样在SELECT语句中计算吗? COUNT(release_date <= CURDATE())
答案 0 :(得分:1)
也许试试这个:
SELECT
total_score / (SELECT COUNT(release_date) FROM movies where release_date <= CURDATE()) as final_score
from movies
WHERE id = 12
答案 1 :(得分:0)
也许尝试这样的事情: -
SELECT (total_score / score_count) as final_score
FROM movies
CROSS JOIN (SELECT COUNT(*) as score_count
FROM movies
WHERE release_date <= CURDATE()) AS Sub1
WHERE id = 12
答案 2 :(得分:0)
一个不起作用的原因是你只选择一行(其中id = 12)。您需要从表中选择两次。一次获取id为12的total_score,另一次计算CURDATE之前的行数。尝试类似:
select A.total_score / count(B.release_date) from movies as A, movies as B
where A.id=12 and B.release_date <= CURDATE();
答案 3 :(得分:0)
根据我的假设,Id是主键(即Movie的ID)。然后查询返回总是单行。现在到final_score取决于发布日期。该发布日期必须早于当前日期,因此以下查询足以实现您的目标,
SELECT total_score / DATEDIFF(CURDATE(), release_date) AS final_score FROM movies WHERE id = 12