I have four tables Like below
Listings:
------------------------------------------------------------------------------------------------
| list_id |user_id | name | category | fees | details |created_on |
------------------------------------------------------------------------------------------------
| 90cc57a4-f782-4c57-ac98-1965c57ece57 |user 100 |satwik| music | 500 | dummy |2015-08-02 |
------------------------------------------------------------------------------------------------
changed my list_id from a random string to UUID.
from this comment on php.net
see this stackoverflow question
In listings table list_id is primary key, i know using autoincreament is best but my requirement is like this. and s.no is primary key for rest of tables.
I need to generate a random key from PHP side because to set list_id in session and to avoid another query to set list_id in session. Likes:
----------------------------------------------------------------
|.sno | list_id | user_id | likes |
----------------------------------------------------------------
| 1 | 90cc57a4-f782-4c57-ac98-1965c57ece57 | user110 | 1 |
----------------------------------------------------------------
| 2 | 90cc57a4-f782-4c57-ac98-1965c57ece57 | user215 | 1 |
----------------------------------------------------------------
| 3 | 90cc57a4-f782-4c57-ac98-1965c57ece57 | user200 | 1 |
----------------------------------------------------------------
comments:
-------------------------------------------------------------------------
|.sno | user_id | list_id | comment |
-------------------------------------------------------------------------
| 1 | user 205| 90cc57a4-f782-4c57-ac98-1965c57ece57 | dummy comment |
-------------------------------------------------------------------------
Views:
----------------------------------------------------------------
|.sno | list_id | user_id | views |
----------------------------------------------------------------
| 1 | 90cc57a4-f782-4c57-ac98-1965c57ece57 | user110 | 2 |
----------------------------------------------------------------
| 2 | 90cc57a4-f782-4c57-ac98-1965c57ece57 | user215 | 1 |
----------------------------------------------------------------
| 3 | 90cc57a4-f782-4c57-ac98-1965c57ece57 | user200 | 1 |
----------------------------------------------------------------
I am trying to get a list of user id user100 from listings table
and need to get count of views,like,comments for the list id of the user from various tables i have.
i tried using this query from codeigniter
$this->db->select ( 'list.*,count(v.views) as views,count(l.likes) as likes,count(c.comment) as comments' )
->from ( 'listings as list' )
->join ( 'views v', 'v.list_id = list.list_id')
->join ( 'likes l', 'l.list_id = list.list_id')
->join ( 'comments c', 'c.list_id = list.list_id');
$this->db->where ( 'list.user_id', $user_id);
$query = $this->db->get ();
I am getting wrong views and likes and comments count.
is this database design good or i need to change anything. i have less awarness in using joins please help me.
EDIT:
I tried this query from answers below
$this->db->select ( 'l.*,count(distinct v.s_no) as views,count(distinct li.s_no) as likes,count(distinct c.s_no) as comments' ,false)
->from ( 'listings as l' )
->join ( 'likes li', 'l.list_id = li.list_id')
->join ( 'comments c', 'l.list_id = c.list_id')
->join ( 'views v', 'l.list_id = v.list_id')
->where ( 'l.user_id', $id);
I am getting what i want but this way of querying fails(return null) if i don't have any comments or views or likes.