Select 行号等于其他 table 中的某个值的行
Select row where row number is equal to some value from other table
我有两个表:游戏和任务
游戏看起来像这样:
| step | manualTaskCounter | autoTaskCounter | (and other)
----------------------------------------------------------
| 1 | 3 | 1 | ...
----------------------------------------------------------
任务看起来像这样:
| id | taskType | taskContent |
-------------------------------
| 1 | M | abc |
| 2 | M | cde |
| 3 | A | efg |
| 4 | M | jpq |
由于任务同时包含手动(使用 M 任务类型)和自动 (A) 任务,因此我想要 select。我的 API 包含两个变量:mTaskCounter 和 aTaskCounter。例如,如果 mTaskCounter = 3 我想 select 来自任务的第 3 行 manualTask 类型。因为它实际上是 id = 4 的行,所以我不能在 WHERE 子句中使用 id。
我已经达到的是:
SELECT
id,
taskType,
taskContent,
(@row:=@row + 1) as rowNumber,
g.manualTaskCounter as mTaskCounter
FROM
tasks t,
(SELECT @ROW:=0) AS r,
(SELECT manualTaskCounter FROM game) AS g
WHERE
g.manualTaskCounter = rowNumber
这表示“where 子句
中的未知列 'rowNumber'
我也尝试过使用 LEFT JOIN:
SELECT
id,
taskType,
taskContent,
(@row:=@row + 1) as rowNumber,
g.manualTaskCounter as mTaskCounter
FROM
tasks t,
(SELECT @ROW:=0) AS r
LEFT JOIN
`game` g ON g.manualTaskCounter = rowNumber
同样的结果。自从我每天使用 mysql 以来已经有一段时间了,不知道如何修复它。我还想制作两个表 - manualTasks 和 autoTasks 而不是任务,所以它可以通过常见的 select taskContent from autoTasks a LEFT JOIN game ON a.id = game.autoTaskCounter
解决问题
为了接近您的目标,首先您需要为手动和自动任务创建派生的table。接下来的查询将使那些 tables 也加起来行号:
Table 有手动任务
SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'M'
Table 有自动任务
SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'A'
现在,您需要做的就是在适当的列上将派生的 table 与 game
table 连接起来:
Select 手动任务编号 X 使用 manualTaskCounter
字段
SELECT
mTasks.*
FROM
game AS g
INNER JOIN
( SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'M' ) AS mTasks ON mTasks.rowNum = g.manualTaskCounter
Select自动任务编号X使用autoTaskCounter
字段
SELECT
aTasks.*
FROM
game AS g
INNER JOIN
( SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'A' ) AS aTasks ON aTasks.rowNum = g.autoTaskCounter
查看下一个在线示例:
我有两个表:游戏和任务
游戏看起来像这样:
| step | manualTaskCounter | autoTaskCounter | (and other)
----------------------------------------------------------
| 1 | 3 | 1 | ...
----------------------------------------------------------
任务看起来像这样:
| id | taskType | taskContent |
-------------------------------
| 1 | M | abc |
| 2 | M | cde |
| 3 | A | efg |
| 4 | M | jpq |
由于任务同时包含手动(使用 M 任务类型)和自动 (A) 任务,因此我想要 select。我的 API 包含两个变量:mTaskCounter 和 aTaskCounter。例如,如果 mTaskCounter = 3 我想 select 来自任务的第 3 行 manualTask 类型。因为它实际上是 id = 4 的行,所以我不能在 WHERE 子句中使用 id。
我已经达到的是:
SELECT
id,
taskType,
taskContent,
(@row:=@row + 1) as rowNumber,
g.manualTaskCounter as mTaskCounter
FROM
tasks t,
(SELECT @ROW:=0) AS r,
(SELECT manualTaskCounter FROM game) AS g
WHERE
g.manualTaskCounter = rowNumber
这表示“where 子句
中的未知列 'rowNumber'我也尝试过使用 LEFT JOIN:
SELECT
id,
taskType,
taskContent,
(@row:=@row + 1) as rowNumber,
g.manualTaskCounter as mTaskCounter
FROM
tasks t,
(SELECT @ROW:=0) AS r
LEFT JOIN
`game` g ON g.manualTaskCounter = rowNumber
同样的结果。自从我每天使用 mysql 以来已经有一段时间了,不知道如何修复它。我还想制作两个表 - manualTasks 和 autoTasks 而不是任务,所以它可以通过常见的 select taskContent from autoTasks a LEFT JOIN game ON a.id = game.autoTaskCounter
为了接近您的目标,首先您需要为手动和自动任务创建派生的table。接下来的查询将使那些 tables 也加起来行号:
Table 有手动任务
SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'M'
Table 有自动任务
SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'A'
现在,您需要做的就是在适当的列上将派生的 table 与 game
table 连接起来:
Select 手动任务编号 X 使用 manualTaskCounter
字段
SELECT
mTasks.*
FROM
game AS g
INNER JOIN
( SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'M' ) AS mTasks ON mTasks.rowNum = g.manualTaskCounter
Select自动任务编号X使用autoTaskCounter
字段
SELECT
aTasks.*
FROM
game AS g
INNER JOIN
( SELECT
t.id,
t.taskType,
t.taskContent,
(@row_num := @row_num + 1) AS rowNum
FROM
tasks AS t
CROSS JOIN
(SELECT @row_num := 0) AS r
WHERE
taskType = 'A' ) AS aTasks ON aTasks.rowNum = g.autoTaskCounter
查看下一个在线示例: