Join votes table and sum all votes - mysql

I've got two tables. One of them contains quotes and the other one lists all given votes (either +1 or -1) for each quote. For demonstration purposes I've made simplified versions of the two tables:
Quotes
+----+-----------------------------------------------------------------------+
| ID | quote |
+----+-----------------------------------------------------------------------+
| 1 | If you stare into the Abyss long enough the Abyss stares back at you. |
| 2 | Don't cry because it's over. Smile because it happened. |
| 3 | Those that fail to learn from history, are doomed to repeat it. |
| 4 | Find a job you love and you'll never work a day in your life. |
+----+-----------------------------------------------------------------------+
Votes
+----+-------+------+
| ID | quote | vote |
+----+-------+------+
| 1 | 1 | -1 |
| 2 | 1 | -1 |
| 3 | 3 | 1 |
| 4 | 3 | -1 |
| 5 | 3 | 1 |
| 6 | 3 | -1 |
| 7 | 4 | 1 |
| 8 | 4 | 1 |
| 9 | 4 | 1 |
+----+-------+------+
I'd like to list all quotes on my site and show the respective vote count besides. At first, the SQL query should read all quotes and afterwards join the votes table. However, it should finally list the sum of all votes for each quote. The result of the SQL query will therefore look as follows:
+----+-----------------+------+
| ID | quote | vote |
+----+-----------------+------+
| 1 | If you stare... | -2 |
| 2 | Don't cry... | NULL |
| 3 | Those that... | 0 |
| 4 | Find a job... | 3 |
+----+-----------------+------+
How does the SQL query look like that does the previously described?

SELECT
`quotes`.`id` as `ID`,
`quote`.`quote` as `quote`,
SUM(`votes`.`vote`) AS `vote`
FROM `quotes`
LEFT JOIN `votes`
ON `quotes`.`id` = `votes`.`quote`
GROUP BY `quotes`.`id`
should do the trick.
Assuming id columns are primary keys (they are unique for each record).

SELECT
ID, quote, (SELECT sum(vote) from votes where votes.quote=quotes.ID)
FROM
quotes

SELECT q.id, q.quote, SUM(v.vote ) as summ
FROM Quotes q
LEFT JOIN Votes v ON q.id=v.quote
GROUP BY q.id, q.quote ;

The following should work. The left join means the vote summaries are included even if there is no line.
select ID, quote, total_votes from quotes
left join
(select quote, sum(vote) as total_votes from quotes
group by quote) ) as vote_totals
on quotes.ID = vote_totals.quote

Select q.id, q.quote, sum(v.vote) from
quotes q
inner join votes v on
q.id= v.quote
group by v.quote

SELECT Quotes.ID ID, Quotes.QUOTE QUOTE, SUM(Votes.vote) VOTE FROM Quotes LEFT JOIN Votes ON Votes.quote = Quotes.id

Related

MySQL select unique rows in two columns with the highest value in one column

I have a basic table:
+-----+--------+------+------+
| id, | name, | cat, | time |
+-----+--------+------+------+
| 1 | jamie | 1 | 100 |
| 2 | jamie | 2 | 100 |
| 3 | jamie | 1 | 50 |
| 4 | jamie | 2 | 150 |
| 5 | bob | 1 | 100 |
| 6 | tim | 1 | 300 |
| 7 | alice | 4 | 100 |
+-----+--------+------+------+
I tried using the "Left Joining with self, tweaking join conditions and filters" part of this answer: SQL Select only rows with Max Value on a Column but some reason when there are records with a value of 0 it breaks, and it also doesn't return every unique answer for some reason.
When doing the query on this table I'd like to receive the following values:
+-----+--------+------+------+
| id, | name, | cat, | time |
+-----+--------+------+------+
| 1 | jamie | 1 | 100 |
| 4 | jamie | 2 | 150 |
| 5 | bob | 1 | 100 |
| 6 | tim | 1 | 300 |
| 7 | alice | 4 | 100 |
+-----+--------+------+------+
Because they are unique on name and cat and have the highest time value.
The query I adapted from the answer above is:
SELECT a.name, a.cat, a.id, a.time
FROM data A
INNER JOIN (
SELECT name, cat, id, MAX(time) as time
FROM data
WHERE extra_column = 1
GROUP BY name, cat
) b ON a.id = b.id AND a.time = b.time
The issue here is that ID is unique per row you can't get the unique value when getting the max; you have to join on the grouped values instead.
SELECT a.name, a.cat, a.id, a.time
FROM data A
INNER JOIN (
SELECT name, cat, MAX(time) as time
FROM data
WHERE extra_column = 1
GROUP BY name, cat
) b ON A.Cat = B.cat and A.Name = B.Name AND a.time = b.time
Think about it... So what ID is mySQL returning form the Inline view? It could be 1 or 3 and 2 or 4 for jamie. Hows does the engine know to pick the one with the max ID? it is "free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. " it could pick the wrong one resulting in incorrect results. So you can't use it to join on.
https://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
If you want to use a self join, you could use this query:
SELECT
d1.*
FROM
date d1 LEFT JOIN date d2
ON d1.name=d2.name
AND d1.cat=d2.cat
AND d1.time<d2.time
WHERE
d2.time IS NULL
It is very simple
SELECT MAX(TIME),name,cat FROM table name group by cat

Select a MYSQL result where tables one and two match and two and three do not match

I have a two tables with users in an old format and a new format. I want to match the users with the old format to a separate table, then exclude all users who also show up in the new user format table. My data is like this:
Table newUsers:
+----+-------+-------+----------+
| id | oldid | first | last |
+----+-------+-------+----------+
| 1 | 10 | John | Kennedy |
| 2 | 66 | Mitch | Kupchak |
+----+-------+-------+----------+
Table posts:
+----+---------+
| id | user_id |
+----+---------+
| 1 | 10 |
| 1 | 66 |
| 1 | 88 |
| 2 | 88 |
| 2 | 28 |
| 3 | 10 |
+----+---------+
Table oldUsers:
+----+----------+-------+----------+
| id | username | first | last |
+----+----------+-------+----------+
| 10 | A | John | Kennedy |
| 66 | B | Mitch | Kupchak |
| 88 | C | Dale | Earnhardt|
+----+----------+-------+----------+
Result wantend:
+----+----------+-------+----------+
| id | username | first | last |
+----+----------+-------+----------+
| 88 | C | Dale | Earnhardt|
+----+----------+-------+----------+
I want to select my result by specifying: posts.id = 1 and posts.user_id = oldUsers.id and newUsers.oldid != oldUsers.id so that I only receive oldUser.id equaling 88 because he wasn't in the newUsers list.
I have tried all kinds of JOINS and SUBQUERIES. I keep getting all of the results and not the results minus corresponding entries in the newUsers table.
select * from oldusers where id in
select * from
(select id from oldusers where id in
select distinct userid from posts where id=1)
where id not in (select oldid from newusers);
Here is a way to do it
select
o.* from oldUsers o
left join newUsers n on o.id = n.oldid
left join posts p on n.oldid = p.user_id or o.id = p.user_id
where n.id is null and p.id= 1;
For better performance add the following indexes
alter table newUsers add index oldid_idx(oldid);
alter table posts add index user_post_idx (id,user_id);
I ended up finding my answer on my own and then came here to find others tried. Abhik's code did work, but was too inefficient to use. I ended up playing with my own code and IS NULL until I found something that was much more efficient.
select o.* from posts p, oldUsers o
LEFT JOIN newUsers n ON o.id = n.oldid
WHERE p.user_id = o.id AND p.id = 1 AND n.id IS NULL
Executes in .0044 seconds. Something I can use on a production site.
With indexes added from previous answer it now executes in .001x seconds so definately going with my own code.

Mysql count records grouped by ID in multiple tables

I'm developing an application integrated with facebook. This application can be embedded in FB page as tab app.
Using FB SDK feeds of page will be stored in Feeds table.
Page fans will may have liked and commented on feeds posted by page.
Users' likes store in Like Table and users' comments store in Comment table
I want to get total count ( Likes count + comment count) of each users'.
SQL Fiddle : http://sqlfiddle.com/#!2/ecb37/10/0
Table : Feeds
| ID | POST_ID |
|----|---------------------------------|
| 56 | 150348635024244_795407097185058 |
| 55 | 150348635024244_795410940518007 |
| 54 | 150348635024244_795414953850939 |
| 53 | 150348635024244_797424133650021 |
| 52 | 150348635024244_797455793646855 |
| 51 | 150348635024244_798997120159389 |
| 50 | 150348635024244_798997946825973 |
Table : Likes
SELECT user_id, COUNT(*) FROM likes GROUP by user_id
| USER_ID | LIKECOUNT |
|------------------|-----------|
| 913403225356462 | 4 |
| 150348635024244 | 3 |
| 356139014550882 | 2 |
| 753274941400012 | 2 |
| 1559751687580867 | 1 |
Table : Comments
SELECT user_id, COUNT(*) FROM comments GROUP by user_id
| USER_ID | COMMENTSCOUNT |
|-----------------|---------------|
| 150348635024244 | 2 |
| 356139014550882 | 2 |
| 913403225356462 | 2 |
Result should be like this
| POINTS | LIKESCOUNT | COMMENTSCOUNT | USER_ID |
|--------|------------|---------------|-----------------|
| 6 | 4 | 2 | 913403225356462 |
| 5 | 3 | 2 | 150348635024244 |
| 4 | 2 | 2 | 356139014550882 |
| 2 | 2 | 0 | 753274941400012 |
| 1 | 1 | 0 |1559751687580867 |
I tried this query. but count of each user's is wrong
SELECT COUNT(likes.user_id)+COUNT(comments.user_id) as points, likes.user_id FROM `likes`
LEFT JOIN comments ON likes.user_id = comments.user_id
LEFT JOIN feeds ON likes.post_id = feeds.post_id
WHERE likes.post_id LIKE '153548635024244%'
GROUP BY likes.user_id
ORDER BY points DESC
The two queries are unrelated and a join is useless. Use a UNION ALL:
SELECT user_id, sum(n) from (
SELECT user_id, COUNT(*) n FROM likes GROUP by user_id
UNION ALL
SELECT user_id, COUNT(*) FROM comments GROUP by user_id
) x
GROUP BY user_id
UNION ALL is needed instead of just UNION, because UNION removes duplicates and would cause incorrect results for the edge case of the two subqueries yielding the same counts.
The simple way to get what you want is to use count(distinct). But that will likely have lousy performance. Instead, use correlated subqueries:
SELECT COUNT(*) +
(select COUNT(c.user_id) from comments c where c.user_id = l.user_id)
) as points, l.user_id
FROM likes l
WHERE l.post_id LIKE '153548635024244%'
GROUP BY l.user_id
ORDER BY points DESC;
I'm not sure what the feeds table is for. However, you version of the query creates a cartesian product between the different tables. If you have a lot of activity for a given user, that would be very bad for performance.

Stuck with mysql join with count query

I'm having a problem with joining 2 queries but in the second query I only want to bring in the count.
This first query works well
SELECT DISTINCT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, members.first_name, views
FROM forum_sub, members
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1
ORDER BY forum_sub.timestamp DESC
which displays the following
----------------------------------------------------------------------
sub_id | forum_id | title | text | url | userid | first_name | views |
----------------------------------------------------------------------
20 | 1 | test | test |test | 1001 | JOhn | 123 |
----------------------------------------------------------------------
1 | 1 | test | test |test | 1002 | Pete | 23 |
----------------------------------------------------------------------
10 | 1 | test | test |test | 1003 | Harry | 34 |
----------------------------------------------------------------------
But now I want to join the above sub_id to another table called forum_topics and count how many of the same sub_id's there are and bring in that value
for example I could use
SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id
-----------------
|sub_id | topics|
---------------
| 1 | 4 |
-----------------
| 10 | 3 |
-----------------
| 20 | 5 |
-----------------
My question is how can I join those 2 queries so I get something like this
----------------------------------------------------------------------------
sub_id | forum_id | title | text | url | userid | first_name | views | count|
-----------------------------------------------------------------------------
20 | 1 | test | test |test | 1001 | JOhn | 123 | 5 |
-----------------------------------------------------------------------------
1 | 1 | test | test |test | 1002 | Pete | 23 | 4 |
-----------------------------------------------------------------------------
10 | 1 | test | test |test | 1003 | Harry | 34 | 3 |
-----------------------------------------------------------------------------
Any help would be great, I know I need to use a subquery but I've been stuck on this nearly all day with no luck
First avoid SELECT DISTINCT when ever possible. It is evil. It will hide cross joins etc that you have in your query. From this query it is hard to tell what exactly your are doing.
However to include the count you have a couple of options:
One would be to do a sub select in the query:
SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid,
members.first_name, views , (SELECT COUNT(sub_id) as topics FROM forum_topics WHERE sub_id = forum_sub.sub_id) count
FROM forum_sub, members
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1
ORDER BY forum_sub.timestamp DESC
Another would be to actually join to the sub query you created:
SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid,
members.first_name, views, counts.topics
FROM forum_sub, members
JOIN (SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id) counts ON (counts.sub_id = forum_sub.sub_id)
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1
ORDER BY forum_sub.timestamp DESC
You should also look at joins instead of selecting multiple tables in your from
SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid,
members.first_name, views, counts.topics
FROM forum_sub
JOIN members ON (members.userid = forum_sub.userid)
JOIN (SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id) counts ON (counts.sub_id = forum_sub.sub_id)
WHERE forum_sub.forum_id = 1
ORDER BY forum_sub.timestamp DESC
Try
group by sub_id
at the end of your second query instead of WHERE.
Then join it to the other table.

LIMIT results to n unique column values?

I have some MySQL results like this:
---------------------------
| name | something_random |
---------------------------
| john | ekjalsdjalfjkldd |
| alex | akjsldfjaekallee |
| alex | jkjlkjslakjfjflj |
| alex | kajslejajejjaddd |
| bob | ekakdie33kkd93ld |
| bob | 33kd993kakakl3ll |
| paul | 3k309dki595k3lkd |
| paul | 3k399kkfkg93lk3l |
etc...
This goes on for 1000's of rows of results. I need to limit the number of results to the first 50 unique names. I think there is a simple solution to this but I'm not sure.
I've tried using derived tables and variables but can't quite get there. If I could figure out how to increment a variable once every time a name is different I think I could say WHERE variable <= 50.
UPDATED
I've tried the Inner Join approach(es) suggested below. The problem is this:
The subselect SELECT DISTINCT name FROM testTable LIMIT 50 grabs the first 50 distinct names. Perhaps I wasn't clear enough in my original post, but this limits my query too much. In my query, not every name in the table is returned in the result. Let me modify my original example:
----------------------------------
| id | name | something_random |
----------------------------------
| 1 | john | ekjalsdjalfjkldd |
| 4 | alex | akjsldfjaekallee |
| 4 | alex | jkjlkjslakjfjflj |
| 4 | alex | kajslejajejjaddd |
| 6 | bob | ekakdie33kkd93ld |
| 6 | bob | 33kd993kakakl3ll |
| 12 | paul | 3k309dki595k3lkd |
| 12 | paul | 3k399kkfkg93lk3l |
etc...
So I added in some id numbers here. These ID numbers pertain to the people's names in the tables. So you can see in the results, not every single person/name in the table is necessarily in the result (due to some WHERE condition). So the 50th distinct name in the list will always have an ID number higher than 49. The 50th person could be id 79, 234, 4954 etc...
So back to the problem. The subselect SELECT DISTINCT name FROM testTable LIMIT 50 selects the first 50 names in the table. That means that my search results will be limited to names that have ID <=50, which is too constricting. If there are certain names that don't show up in the query (due to some WHERE condition), then they are still counted as one of the 50 distinct names. So you end up with too few results.
UPDATE 2
To #trapper: This is a basic simplification of what my query looks like:
SELECT
t1.id,
t1.name,
t2.details
FROM t1
LEFT JOIN t2 ON t1.id = t2.some_id
INNER JOIN
(SELECT DISTINCT name FROM t1 ORDER BY id LIMIT 0,50) s ON s.name = t1.name
WHERE
SOME CONDITIONS
ORDER BY
t1.id,
t1.name
And my results look like this:
----------------------------------
| id | name | details |
----------------------------------
| 1 | john | ekjalsdjalfjkldd |
| 3 | alex | akjsldfjaekallee |
| 3 | alex | jkjlkjslakjfjflj |
| 4 | alex | kajslejajejjaddd |
| 6 | bob | ekakdie33kkd93ld |
| 6 | bob | 33kd993kakakl3ll |
| 12 | paul | 3k309dki595k3lkd |
| 12 | paul | 3k399kkfkg93lk3l |
...
| 37 | bill | kajslejajejjaddd |
| 37 | bill | ekakdie33kkd93ld |
| 41 | matt | 33kd993kakakl3ll |
| 50 | jake | 3k309dki595k3lkd |
| 50 | jake | 3k399kkfkg93lk3l |
----------------------------------
The results stop at id=50. There are NOT 50 distinct names in the list. There are only roughly 23 distinct names.
My MySql syntax may be rusty, but the idea is to use a query to select the top 50 distinct names, then do a self-join on name and select the name and other information from the join.
select a.name, b.something_random
from Table b
inner join (select distinct name from Table order by RAND() limit 0,50) a
on a.name = b.name
SELECT DISTINCT name FROM table LIMIT 0,50
Edited: Ahh yes I misread question first time, this should do the trick though :)
SELECT a.name, b.something_random
FROM `table` b
INNER JOIN (SELECT DISTINCT name FROM `table` ORDER BY RAND() LIMIT 0,50) a
ON a.name = b.name ORDER BY a.name
How this work is the (SELECT DISTINCT name FROMtableORDER BY RAND() LIMIT 0,50) part is what pulls out the names to include in the join. So here I am taking 50 unique names at random, but you can change this to any other selection criteria if you want.
Then you join those results back into your table. This links each of those 50 selected names back to all of the rows with a matching name for your final results. Finally ORDER BY a.name just to be sure all the rows for each name end up grouped together.
This should do it:
SELECT tA.*
FROM
testTable tA
INNER JOIN
(SELECT distinct name FROM testTable LIMIT 50) tB ON tA.name = tB.name
;