假设我们有2个相关的表
Table1:
id item_id(References to Table2:id) rating
Table2:
id title day position
我需要查询一个查询: 对于所有Table2项目,检查表1:评级并将其位置(ASC)置于表1:位置,但仅限于特定日期。每一天 - 新的评级。 1,2 ... 1000
Exmpl:
Table1:
id item_id rating
1 1 2
2 2 1555
3 3 280
我们需要得到:
Table2:
id title day position
1 test1 1 1
2 test2 1 3
3 test3 1 2
N .... 2 ...
答案 0 :(得分:1)
怎么样?
SELECT a.*, b.*
FROM table2 a
INNER JOIN table1 b
ON a.id = b.item_id
WHERE day BETWEEN day_1 AND day_2
ORDER BY a.position ASC
尝试一下,
UPDATE table2 e
INNER JOIN
(
SELECT c.*,
@rank:=@rank+1 row_rank
FROM
(
SELECT a.*, b.*
FROM table2 a
INNER JOIN table1 b
ON a.id = b.item_id
WHERE day BETWEEN day_1 AND day_2
) c, (SELECT @rank:=0) d
ORDER BY c.rating ASC
) f ON e.ID = f.ID
SET e.position = f.row_rank