22,300
社区成员




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
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
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]
对了 我这个语句是需要排序的。用了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]
对了 我这个语句是需要排序的。用了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这个是合并结果集,同时去重的,所以就把重复的结果给去掉了 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]
对了 我这个语句是需要排序的。用了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]
对了 我这个语句是需要排序的。用了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*权限表该商品条目数。 而用这一整句放进去 却只有两条呢?
但是由于我本来就只要这样的两条所以这个语句是没问题的。 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
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'