0

我有这个查询,但这显示列不存在

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

4

3 回答 3

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
于 2012-06-27T11:46:48.967 回答
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 子句,作为另一个结果集

于 2012-06-27T11:45:29.370 回答
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'
于 2012-06-27T11:53:52.167 回答