我有这个查询,但这显示列不存在
SELECT
p.content,
(select count(*) from share_member WHERE username='pratik' AND share_id=share) as allow
FROM posts p WHERE allow >="1"
我收到了这个错误
'where子句'中的未知列'allow'
修改 已完成此编辑以改进我的使用的SQL查询
SELECT
p.content,
CASE share_type
WHEN 'public' then "1"
when 'lsit' then
(select count(*) from share_member WHERE username='pratik' AND share_id=share)
end as allow
FROM posts p WHERE allow=>1
错误代码1064,SQL状态42000
答案 0 :(得分:2)
您不能在WHERE
子句中使用列别名。
但是,您可以使用HAVING
子句,在那里可以使用别名。区别在于在构造结果集时执行WHERE
,并且在匹配整个集合之后应用HAVING
(这意味着HAVING
更重要)。
SELECT
p.content,
(select count(*) from share_member WHERE username='pratik' AND share_id=share) as allow
FROM posts p
HAVING allow >= 1
编辑:您的第二个查询应如下所示:
SELECT p.content,
CASE
WHEN share_type='public' THEN "1"
WHEN share_type='list' THEN (SELECT COUNT(*) FROM share_member WHERE username='pratik' AND share_id=share)
END AS `allow`
FROM posts p
HAVING `allow` >= 1
答案 1 :(得分:1)
应该是
SELECT p.content,
(select count(*)
from share_member
WHERE username='pratik' AND share_id=share) as allow
FROM posts p
WHERE (select count(*)
from share_member
WHERE username='pratik' AND share_id=share) >=1
或
(select count(*)
from share_member
WHERE username='pratik' AND share_id=share)
应该是from子句,作为另一个结果集
答案 2 :(得分:0)
您的查询已修改为
SELECT
p.content,
(select count(*) from share_member WHERE username='pratik' AND share_id='share') as allow
FROM posts p WHERE allow >=1
SELECT
p.content,
(select count(*) from share_member WHERE username='pratik' AND share_id='share') as allow
FROM posts p WHERE allow >='1'