情况如下
要求 根据 UserId 取出 SysMenus.Id=MenuId 然后通过 SysMenus.pid=SysMenus.id得出记录
请问这样的sql语句怎么写,最后转换为存储过程
我这样编写 没有通过
select distinct a.Id, a.Title, a.Target, a.FullName, a.Img, a.Sata, a.Url, a.Pid
from SysMenus a,UserMenus b where a.Id in (select b.MenuId from dbo.UserMenus where b.UserId=@userId) and a.pid=@pid
select distinct a.Id, a.Title, a.Target, a.FullName, a.Img, a.Sata, a.Url, a.Pid
from SysMenus a,UserMenus b where a.Id in (select b.MenuId from dbo.UserMenus where b.UserId=@userId) and a.pid=@pid
select distinct a.Id, a.Title, a.Target, a.FullName, a.Img, a.Sata, a.Url, a.Pid
from SysMenus a where a.pid in (select MenuId from dbo.UserMenus where UserId=@userId)
你这样写查询语句试试,是不是你想要的
select distinct a.Id, a.Title, a.Target, a.FullName, a.Img, a.Sata, a.Url, a.Pid
from SysMenus a
inner join UserMenus b on a.Id=b.MenuId and a.Pid=b.Id
where b.UserId=@userId
后你把它改写成存储过程
谢谢 我改成功了
select distinct a.Id, a.Title, a.Target, a.FullName, a.Img, a.Sata, a.Url, a.Pid,b.Id as umid,b.UserId
from SysMenus a
inner join UserMenus b on b.MenuId=a.Id
where b.UserId=1 and pid=4
@SpeakHero: 这是你自己的头像吗
@Albert Fei: ....
@SpeakHero: 省略号代表是吗?
create procedure UP_GetMenus
@UserId int
as
select Id, Title, Target, FullName, Img, Sata, Url, Pid
from SysMenus where a.Id in (select MenuId from UserMenus where UserId=@UserId) and pid=Id