select `shops`.*, (select distinct count(*) from `views` where `shops`.`id` = `views`.`viewable_id` and `views`.`viewable_type` = 'App\Shop') as `views_count` from `shops` where `user_id` = 1 order by `id` asc limit 10 offset 0
Why distinct is not working?
I need get: count(distinct visitor), but not distinct count(*)
distinct actually doesn't except a column, it will insert whatever column you're trying to count automatically. However, I think this is going to generate two queries, this should work: