mysql 表别名 子查询_MYSQL:我可以在子查询中使用外部查询表别名

bd96500e110b49cbb3cd949968f18be7.png

Here is my query:

select uact.onAssetID as AssetID, a1.value as AssetValue,

uact.CommentID, a2.value from useractivity uact inner join asset a1 on

uact.onAssetID=a1.ID inner join (select * from asset inner join

useractivity on uact.onAssetID=a1.ID group by a1.ID limit 3) a2 on

uact.CommentID=a2.ID;

Error: ERROR 1054 (42S22): Unknown column 'uact.onassetID' in 'on clause'

What I am trying do?

Imagine there is Table1 with 10 student names, now there is Table2 which has 10 records(rows) for each of the student present in table1. If I try to use join on both tables, it will return 100 records(10 records for each student). What I want is, to limit the result returned by join by 5 records per students

So, I should get 5 records per student hence 50 output rows

Table Structure(asset):

ID | TypeID | CategoryID | Worth | isActive

| CreationDate | ExpiryDate Value | AssetOwner

Table Structure(useractivity)

| ID | ActivityTypeID | UserID | Time | onAssetID | CommentID

Notes:

1) ID for both table is a primary key

2) onAssetID and commentID in useractivity are foriegn keys referring to ID in asset Table

Feel free to let me know if you need more details

解决方案What I am trying do? Imagine there is Table1 with 10 student names,

now there is Table2 which has 10 records(rows) for each of the student

present in table1. If I try to use join on both tables, it will return

100 records(10 records for each student). What I want is, to limit the

result returned by join by 5 records per students So, I should get 5

records per student hence 50 output rows

Answer for this:-

Table1(studentid,.......)

Table2(id,.........,studentid)

select s.*,temp.* from (SELECT @var:=if(@var2= a.studentid,@var+1,1) sno, @var2:= studentid, a.*

FROM Table2 a,(select @var:=0) b, (select @var2:=0) c

order by studentid) temp, Table1 s where temp.sno<=5;

For your table structures:- Table Structure(asset): ID | TypeID |

CategoryID | Worth | isActive | CreationDate | ExpiryDate Value |

AssetOwner Table Structure(useractivity): | ID | ActivityTypeID |

UserID | Time | onAssetID | CommentID

select s.*,temp.*

from

(SELECT @var:=if(@var2= a.onAssetID,@var+1,1) sno, @var2:= onAssetID, a.*

FROM useractivity a,(select @var:=0) b, (select @var2:=0) c

order by onAssetID) temp, asset s

where temp.sno<=5;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值