当前位置: 代码迷 >> Sql Server >> 一个判断是否是权限商品,以后进行不同筛选的SQL语句
  详细解决方案

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

热度:70   发布时间:2016-04-24 08:54:38.0
一个判断是否是权限商品,之后进行不同筛选的SQL语句
假设这是product 表 表里有商品的信息 及id 还有这个是否需要授权

然后这是权限表 

pid对应product 表 的id

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

关键就是 如何选出  is_access为false 和  is_access为true 且在权限表中的 商品
------解决思路----------------------
    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

------解决思路----------------------
引用:
Quote: 引用:


试试这个;
		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貌似是不能排序的?



可以的,关键看你要怎么排序,比如:
		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

  相关解决方案