[英]Select row where row number is equal to some value from other table
I have two tables: game and tasks 我有两个表:游戏和任务
Game looks like this: 游戏看起来像这样:
| step | manualTaskCounter | autoTaskCounter | (and other)
----------------------------------------------------------
| 1 | 3 | 1 | ...
----------------------------------------------------------
Tasks looks like this: 任务如下所示:
| id | taskType | taskContent |
-------------------------------
| 1 | M | abc |
| 2 | M | cde |
| 3 | A | efg |
| 4 | M | jpq |
Since tasks holds both, manual (with M taskType) and automatic (A) tasks I want to select. 由于任务同时包含我要选择的手动任务(带有M taskType)和自动任务(A)。 My API holds two variables: mTaskCounter and aTaskCounter.
我的API包含两个变量:mTaskCounter和aTaskCounter。 for example if mTaskCounter = 3 I want to select 3rd row of type manualTask from tasks.
例如,如果mTaskCounter = 3,我想从任务中选择manualTask类型的第三行。 Since it is in fact row with id = 4 I can not use id in WHERE clause.
因为它实际上是id = 4的行,所以我不能在WHERE子句中使用id。
What I already achieved is: 我已经实现的是:
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
This says "unknown column 'rowNumber' in where clause 这表示where子句中的“未知列'rowNumber'
I also tried to use LEFT JOIN: 我也尝试使用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
Same result. 结果相同。 It's been a while since I used mysql everyday and dont know how to fix it.
自从我每天都使用mysql并且不知道如何解决它以来已经有一段时间了。 I also think to make two tables - manualTasks and autoTasks instead of tasks so it qould solve the problem by common
select taskContent from autoTasks a LEFT JOIN game ON a.id = game.autoTaskCounter
我还认为要制作两个表-manualTasks和autoTasks而不是任务,因此它可以通过
select taskContent from autoTasks a LEFT JOIN game ON a.id = game.autoTaskCounter
常见的select taskContent from autoTasks a LEFT JOIN game ON a.id = game.autoTaskCounter
解决问题select taskContent from autoTasks a LEFT JOIN game ON a.id = game.autoTaskCounter
For approaching your goal, first you will need to make derived tables for both manual and automatic tasks. 为了实现您的目标,首先您需要为手动和自动任务制作派生表。 Next queries will made those tables adding up the row number too:
接下来的查询将使这些表也累加行号:
Table With Manual Tasks 带有手动任务的表
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 With Automatic Tasks 带有自动任务的表
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'
Now, all you need to do is join those derived tables with the game
table on the adequate columns: 现在,您需要做的就是将这些派生表与
game
表连接到足够的列上:
Select manual task number X using the manualTaskCounter
field 使用
manualTaskCounter
字段选择手动任务编号X
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 automatic task number X using the autoTaskCounter
field 使用
autoTaskCounter
字段选择自动任务编号X
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
Check the next online example: 查看下一个在线示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.