-3

假设,我们有 2 个相关的表

Table1:
id item_id(References to Table2:id) rating

Table2:
id title day position

我需要查询:对于所有 Table2 项目,检查 Table1:rating 并将其位置(ASC)放到 Table1:position 但仅针对特定日期。每天 - 新评级。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   ...
4

1 回答 1

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
于 2013-02-04T07:40:41.553 回答