SQL - placing condition in where clause
Earlier on today I asked this question at SO: Count() and left join problem
As indicated, the correct query for my problem is:
select s.name
, p.name
, count(p.id)
from Shop as s
left join Product as p on p.shop=s.id AND p.status <> '8796107276379'
group by
s.id, p.name
We are using a proprietary framework which allows us to add restrictions to queries running on the system. I am quite sure that it is being achieved through a where
clause being appended at the end of the queries.
The problem is can I translate the above query in such a manner that p.status <> '8796107276379'
is in a where
clause so that I could add this as a restriction?
The other answers at Count() and left join problem all put the开发者_开发百科 condition in the where
clause but none of them worked.
Any ideas?
Thanks in advance!
Krt_Malta
select s.name
, p.name
, count(p.id)
from Shop as s
left join Product as p on p.shop=s.id
where
p.shop is null or p.status <> '8796107276379'
group by
s.id, p.name
This will work on Oracle and MySQL, but it might fail on SQL Server (not sure).
Sure... (for SQL Server)
select s.name
, p.name
, count(p.id)
from Shop as s
left join Product as p on p.shop=s.id
where IsNull(p.status,'') <> '8796107276379'
group by
s.id, p.name
精彩评论