I have three tables that I join. submissions , submissions_votes and users .
I want to find out how much useful useful data there is (which is the sum of counting all submissions_votes ), and I got this.
I also want to return the counter (boolean, rather) 0 or 1 if user_id of sv.user_id refers to view view. user_id is passed to the WHERE .
SELECT s.*, u.username, u.photo as userPhoto, COALESCE(SUM(sv.up), 0) helpfulVotes FROM submissions s LEFT JOIN submissions_votes sv on s.id = sv.submission_id WHERE u.id = ? INNER JOIN users u ON s.user_id = u.id
I know I need an additional connection (on sv.user_id = u.id ), but what would I choose? Then would I group using sv.id ?
Edit:
users table:
+----------------+------------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +----------------+------------------------+------+-----+-------------------+-----------------------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | email | varchar(128) | NO | MUL | NULL | | | username | varchar(23) | NO | | NULL | | | type | enum('normal','admin') | NO | | normal | | | about | varchar(255) | NO | | NULL | | | photo | varchar(32) | NO | | NULL | | +----------------+------------------------+------+-----+-------------------+-----------------------------+
submissions_votes table:
+---------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | submission_id | int(10) unsigned | NO | MUL | NULL | | | when | datetime | NO | | NULL | | | user_id | int(10) unsigned | NO | MUL | NULL | | | up | tinyint(3) unsigned | NO | | NULL | | | down | tinyint(3) unsigned | NO | | NULL | | +---------------+---------------------+------+-----+---------+----------------+
submissions table:
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | title | varchar(255) | NO | MUL | NULL | | | slug | varchar(255) | NO | | NULL | | | description | mediumtext | NO | | NULL | | | user_id | int(11) | NO | MUL | NULL | | | created | datetime | NO | | NULL | | | type | enum('tip','request') | NO | | NULL | | | thumbnail | varchar(64) | YES | | NULL | | | removed | tinyint(1) unsigned | NO | | 0 | | | keywords | varchar(255) | NO | | NULL | | | ip | int(10) unsigned | NO | | NULL | | +-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+-----+---------+----------------+
source share