我正在将Google Cloud Platform与mySQL一起使用。
我有一个包含3列的表格:
idUser, Name, timestamp
me@you.com, Mike Jones, 1-6-1900 05:00
us@we.com, Steve Brown, 1-2-1900 06:00
jsmith@email.com, John Smith, 1-5-1900 06:00
test@me.com, Sarah Lee, 1-3-1900 05:30
我想基于时间戳作为订单/排序来查询“可以找到jsmith@email.com所在的行号”。
我已经能够部分工作了,我可以根据时间戳生成一些显示行号的结果:
SET @row_number:=0;
SELECT @row_number:=@row_number+1 AS row_number,idUser FROM SpeakerQueueActive
ORDER BY timestamp
这使我得到添加行号的结果:
row_num, idUser, Name, timestamp
1,us@we.com, Steve Brown, 1-2-1900 06:00
2,test@me.com, Sarah Lee, 1-3-1900 05:30
3,jsmith@email.com, John Smith, 1-5-1900 06:00
4,me@you.com, Mike Jones, 1-6-1900 05:00
我想我现在想将其嵌套并使用类似的东西:
SELECT * from
(SET @row_number:=0;
SELECT @row_number:=@row_number+1 AS row_number,idUser FROM SpeakerQueueActive
ORDER BY timestamp)
WHERE idUser = 'jsmith@email.com'
但是我收到一个语法错误的错误消息。 我还尝试将第一段代码存储为一个存储过程,然后调用它,但是我似乎也无法将其与SELECT一起使用。
所以回到我的原始问题,我想向我报告他在第3行中
我该怎么做? 谢谢!
答案 0 :(得分:0)
您需要表名别名FROM ( ) t
SET @row_number:=0;
SELECT t.* from (
SELECT @row_number:=@row_number+1 AS row_number,idUser FROM SpeakerQueueActive
ORDER BY timestamp) t /* here add the table name alias */
WHERE t.idUser = 'jsmith@email.com'
答案 1 :(得分:0)
SELECT * from
(SET @row_number:=0;
SELECT @row_number:=@row_number+1 AS row_number,idUser FROM SpeakerQueueActive
ORDER BY timestamp)
WHERE idUser = 'jsmith@email.com'
需要将其重写为
SELECT * from (
SELECT @row_number:=@row_number+1 AS row_number,idUser FROM SpeakerQueueActive
CROSS JOIN (SELECT @row_number:= 0) AS init
ORDER BY timestamp) AS alias
WHERE idUser = 'jsmith@email.com'
或作为基于逗号的CROSS JOIN。
SELECT * from (
SELECT @row_number:=@row_number+1 AS row_number,idUser FROM SpeakerQueueActive
, (SELECT @row_number:= 0) AS init
ORDER BY timestamp) AS alias
WHERE idUser = 'jsmith@email.com'