Beginner here! I am trying to write a query that will select the 3 most commented on results from a "results" table the comments are stored in a seperate "comments" table.
results
- id
- title
- body
- etc
- etc
comments
- id
- result_id
- user_id
- timestamp
- comment
So I need to select all from results and order by the amount of matches between results.id and comments.result_id but I don't really know where to start!
Thanks a lot for the help, it's much appreciated!
Not tested but you can do something like that
SELECT r.id ,r.title, r.body
FROM results r INNER JOIN (SELECT result_id, count(id) cnt FROM comments GROUP BY result_id) c
ON r.id = c.result_id
ORDER by c.cnt DESC
Perhaps try something like this:
SELECT COUNT(c.id) AS comment_count FROM results r
LEFT JOIN comments c ON r.id=c.result_id
GROUP BY result_id ORDER BY comment_count DESC LIMIT 3;
The following should work:
SELECT r.id, COUNT(r.id) AS comment_count
FROM results r
INNER JOIN comments c
ON results.id = c.result_id
GROUP BY r.id
ORDER BY comment_count DESC
You join the two tables where the id of the result is the same as the referenced result_id from the comments table. Then you group the rows by result_id to remove duplicates. The COUNT() function sums up the grouped rows and displays the number of them.
Then just sort the result based on the generated comment count.
You could use LEFT OUTER JOIN as well, then you would get all results that have no comments as well. If you want this or not depends on your needs.
For a description of SQL joins, check out What is the difference between "INNER JOIN" and "OUTER JOIN"?
I'm fairly new to MYSQL!
I need to make a SQL query where i check how many likes a row has (between two tables)
I found another question that looked like mine, but i can't get it to return anything (even though it doesn't create an error.
query:
SELECT *
FROM likes
INNER JOIN (SELECT likes.like_id,
COUNT(*) AS likes
FROM likes
INNER JOIN uploads ON likes.upload_id=uploads.upload_id
WHERE uploads.upload_date >= DATE_SUB(CURDATE(), INTERVAL 8 DAY)
GROUP BY uploads.upload_id) x ON x.like_id = likes.like_id
ORDER BY x.likes DESC
Link to the original question:
MySQL, Need to select rows that has the most frequent values in another table
Help is much appreciated
Kind regards,
Mathias
Since you didn't post your table structure I'll have to guess..
select someid, count(*) cnt from
(
select * from table1 t1 join table2 t2 on t1.someid = t2.someid
) as q0 group by someid order by cnt desc;
It will need tweaking to fit your schema.
im looking for a way in php to grab entries from a table named 'answers' in a mysql database, then grab entries from a table called 'votes' and calculate from them, the answer with the highest vote. I cant seem to find any help on this.
The structures can be viewed here
How it works is the users vote gets stored in the 'votes' table and is recognized by the 'answer_id' column, i just dont no how count them all and determine the answer with the highest votes
what about
SELECT a.*, COUNT(v.id) tot
FROM answers a INNER JOIN votes v
on a.id = v.answer_id
GROUP BY a.id
ORDER BY tot DESC
If you want to get also answers without any vote, use:
SELECT a.*, COUNT(v.id) tot
FROM answers a LEFT JOIN votes v
on a.id = v.answer_id
GROUP BY a.id
ORDER BY tot DESC
I have a table that stores scores from users of my game - what I want to be able to do if possible is find their rank using mySQL alone (because if the amount of players increases exponentially the php loop times to parse the entire database will increase dramatically).
So far I have been able to get this statement
select #rownum:=#rownum+1 'rank', s.* from top100 s, (select #rownum:=0) r order by score desc
to return a result set with rankings applied - what I then need to be able to do is find a single item within that using a subquery to find the players last insert_id from a previous insert.
Any help would be greatly appreciated.
SELECT t.*,
(SELECT COUNT(*)
FROM top100 t2
WHERE t2.score > t.score) AS rank
FROM top100 t
WHERE id = LAST_INSERT_ID()
dear php and mysql expertor
i have two table one large for posts artices 200,000records (index colume: sid) , and one small table (index colume topicid ) for topics has 20 record .. have same topicid
curent im using : ( it took round 0.4s)
+do get last 50 record from table:
SELECT sid, aid, title, time, topic, informant, ihome, alanguage, counter, type, images, chainid FROM veryzoo_stories ORDER BY sid DESC LIMIT 0,50
+then do while loop in each records for find the maching name of topic in each post:
while ( .. ) {
SELECT topicname FROM veryzoo_topics WHERE topicid='$topic'"
....
}
+Now
I going to use Inner Join for speed up process but as my test it took much longer from 1.5s up to 3.5s
SELECT a.sid, a.aid, a.title, a.time, a.topic, a.informant, a.ihome, a.alanguage, a.counter, a.type, a.images, a.chainid, t.topicname FROM veryzoo_stories a INNER JOIN veryzoo_topics t ON a.topic = t.topicid ORDER BY sid DESC LIMIT 0,50
It look like the inner join do all joining 200k records from two table fist then limit result at 50 .. that took long time..
Please help to point me right way doing this..
eg take last 50 records from table one.. then join it to table 2 .. ect
Do not use inner join unless the two tables share the same primary key, or you'll get duplicate values (and of course a slower query).
Please try this :
SELECT *
FROM (
SELECT a.sid, a.aid, a.title, a.time, a.topic, a.informant, a.ihome, a.alanguage, a.counter, a.type, a.images, a.chainid
FROM veryzoo_stories a
ORDER BY sid DESC
LIMIT 0 , 50
)b
INNER JOIN veryzoo_topics t ON b.topic = t.topicid
I made a small test and it seems to be faster. It uses a subquery (nested query) to first select the 50 records and then join.
Also make sure that veryzoo_stories.sid, veryzoo_stories.topic and veryzoo_topics.topicid are indexes (and that the relation exists if you use InnoDB). It should improve the performance.
Now it leaves the problem of the ORDER BY LIMIT. It is heavy because it orders the 200,000 records before selecting. I guess it's necessary. The indexes are very important when using ORDER BY.
Here is an article on the problem : ORDER BY … LIMIT Performance Optimization
I'm just give test to nested query + inner join and suprised that performace increase much: it now took only 0.22s . Here is my query:
SELECT a.*, t.topicname
FROM (SELECT sid, aid, title, TIME, topic, informant, ihome, alanguage, counter, TYPE, images, chainid
FROM veryzoo_stories
ORDER BY sid DESC
LIMIT 0, 50) a
INNER JOIN veryzoo_topics t ON a.topic = t.topicid
if no more solution come up , i may use this one .. thanks for anyone look at this post