使用row_number

时间:2016-10-21 18:05:28

标签: mysql

大家好。

我有一组具有唯一ID的不同客户,他们花费的金额和购买日期。我想要一张包含所有ID和第一笔订单日期的表格。

例如:

enter image description here

我使用MySQL Workbench 6.3 CE并尝试了以下代码:

SELECT t.amount, t.debtor_id, t.created  
FROM ( SELECT @row_number:=IF(@debtorid=t.debtor_id,@row_number+1,1) AS RowNumber, 
t.debtor_id,
t.created, 
t.amount, 
@debtorid:=t.debtor_id 
FROM capital.transaction t, 
       (SELECT@row_num:=1) x,  
       (SELECT @debtorid :='') y 
order by t.debtor_id, t.created) subquery 

JOIN capital.transaction_event te ON t.id=te.auth_entry_id  
WHERE (fromstate='SPX_APPROVED') and webshop_id='3579' and t.amount>2000 and RowNumber=1

我不断收到消息:“错误代码:1054。字段列表中的未知列't.amount'。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

如果你调用dinamic table子查询,那么你应该用子查询引用相关的selectedc列而不是t

SELECT subquery.amount, subquery.debtor_id, subquery.created  
FROM ( SELECT @row_number:=IF(@debtorid=t.debtor_id,@row_number+1,1) AS RowNumber, 
t.debtor_id,
t.created, 
t.amount, 
@debtorid:=t.debtor_id 
FROM capital.transaction t, 
       (SELECT@row_num:=1) x,  
       (SELECT @debtorid :='') y 
order by t.debtor_id, t.created) subquery 

JOIN capital.transaction_event te ON subquery.id=te.auth_entry_id  
WHERE (fromstate='SPX_APPROVED') and webshop_id='3579' and subquery.amount>2000 and RowNumber=1