一个判断是否是权限商品,之后进行不同筛选的SQL语句

xiaoyang7545 2015-09-16 10:02:35
假设这是product 表 表里有商品的信息 及id 还有这个是否需要授权

然后这是权限表

pid对应product 表 的id

我想要的效果是 假设在 product 表里面 is_access为true的时候 就需要 条件 这个商品id 在权限表里
且 uid=当前用户的uid(这个应该就多一个where,没太多好说的)。

关键就是 如何选出 is_access为false 和 is_access为true 且在权限表中的 商品
...全文
143 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyang7545 2015-09-16
  • 打赏
  • 举报
回复
引用 2 楼 yupeigu 的回复:
试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?
LongRui888 2015-09-16
  • 打赏
  • 举报
回复
试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
Tiger_Zhao 2015-09-16
  • 打赏
  • 举报
回复
    SELECT p.*
FROM product p
LEFT JOIN rights r
ON p.id = r.pid
AND p.is_access = true
AND r.uid = @uid
WHERE p.is_access = false
OR r.id IS NOT NULL
xiaoyang7545 2015-09-16
  • 打赏
  • 举报
回复
引用 6 楼 Tiger_Zhao 的回复:
我#1的可以直接加排序啊。
恩 你的也可以。。我刚刚用你的数据没 出来是 因为true 没加引号。。还以为不行~
LongRui888 2015-09-16
  • 打赏
  • 举报
回复
引用 7 楼 xiaoyang7545 的回复:
[quote=引用 5 楼 yupeigu 的回复:] [quote=引用 3 楼 xiaoyang7545 的回复:] [quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
[/quote]
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 xiaoyang7545 的回复:] [quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
[/quote]
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 xiaoyang7545 的回复:] [quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
[/quote] 用你的 方法 可以实现了 但是我有个疑问 就是为什么 我有2个is_access = true的商品 直接用你union里的第二个子句 查出来 数据条目是有 2*权限表该商品条目数。 而用这一整句放进去 却只有两条呢? 但是由于我本来就只要这样的两条所以这个语句是没问题的。[/quote] 可能是在第二个语句中,关联出来的结果有重复,然后union这个是合并结果集,同时去重的,所以就把重复的结果给去掉了
xiaoyang7545 2015-09-16
  • 打赏
  • 举报
回复
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 xiaoyang7545 的回复:] [quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
[/quote]
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 xiaoyang7545 的回复:] [quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
[/quote]
引用 5 楼 yupeigu 的回复:
[quote=引用 3 楼 xiaoyang7545 的回复:] [quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
[/quote] 用你的 方法 可以实现了 但是我有个疑问 就是为什么 我有2个is_access = true的商品 直接用你union里的第二个子句 查出来 数据条目是有 2*权限表该商品条目数。 而用这一整句放进去 却只有两条呢? 但是由于我本来就只要这样的两条所以这个语句是没问题的。
Tiger_Zhao 2015-09-16
  • 打赏
  • 举报
回复
我#1的可以直接加排序啊。
LongRui888 2015-09-16
  • 打赏
  • 举报
回复
引用 3 楼 xiaoyang7545 的回复:
[quote=引用 2 楼 yupeigu 的回复:] 试试这个;
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
对了 我这个语句是需要排序的。用了union貌似是不能排序的?[/quote] 可以的,关键看你要怎么排序,比如:
		select *
		from 
		(
		select p.*
		from product p
		where p.is_access = false

		union 

		select p.*
		from product p
		inner join rights r
		on p.id = r.pid
		where p.is_access = true
        )t
		order by pid
hhhttt_ 2015-09-16
  • 打赏
  • 举报
回复
select * from product p
  where  exists (
           select top 1 1 from right r where r.uid=p.uid and r.id=p.pid and p.is_access='true' ) 
   or p.is_access='false'

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧