我有这张桌子:
post{id, user_id, event_id}
我想获取特定用户拥有event_id(非NULL)的所有帖子,并且我希望再次发生event_id,我希望它是不同的,即如果2个帖子的event_id为2,那么我只需要其中一个被检索,谢谢!
即
select count(*)
from posts
where user_id = '43'
and
//not null event id
//distinct event id's
答案 0 :(得分:2)
尝试此操作以获取事件ID的计数:
SELECT COUNT(DISTINCT event_id)
FROM posts
WHERE user_id = '43'
AND event_id IS NOT NULL
答案 1 :(得分:1)
尝试
select distinct event_id
from posts
where user_id='43' and event_id is not null
如果你需要id,它会有点复杂,但可以使用这个
select max(id)
from posts
where user_id='43' and event_id is not null
group by event_id
如果你想要第一个事件编号
,你可以用min(id)代替max(id)答案 2 :(得分:1)
select event_id, count(event_id)
from posts
where user_id=43 and event_id is not null
group by event_id;
group by
是为每个event_id