不知道理解的对不对,有问题再问我吧
建立测试数据
CREATE TABLE button(button_id varchar(10))
go
CREATE TABLE [user](USER_ID varchar(10))
go
create TABLE user_button(USER_ID varchar(10),button_id varchar(10))
go
INSERT INTO dbo.button
( button_id )
SELECT
1001
UNION ALL
SELECT
1002
UNION ALL
SELECT
1003
UNION ALL
SELECT
1004
go
INSERT INTO [dbo].[user]
( [USER_ID] )
SELECT
1
UNION ALL
SELECT
2
INSERT INTO dbo.user_button
( USER_ID, button_id )
SELECT 1,1002
UNION ALL
SELECT 1,1003
go
查询语句
SELECT a.button_id,SUM(CASE WHEN isnull(b.USER_ID,'')='' THEN 0 ELSE 1 END) AS checked
FROM
(
SELECT *
FROM [user] cross APPLY [dbo].[button]
) a LEFT JOIN user_button b ON a.USER_ID=b.USER_ID AND a.button_id=b.button_id
GROUP BY a.button_id
结果:
button_id checked
1001 0
1002 1
1003 1
1004 0
select *,'有点击权限' checked
from button where button_id in (select button_id from user_button where user_id=你的ID)
说明一下每个表有哪些字段吧