Here is my table arrangement:
Posts
post_id
post_votes
Comments
comment_id
post_id
comment_time
I have failed to create a query that does the following:
Select 10 Posts Order By post_votes desc
While getting 5 comments for each post
I will post what I have tried if necessary.
I am just getting into more complicated queries, any help or suggestions is greatly appreciated
The below will retrieve 10 post order by desc and also 5 comments order by desc respectively.
select post_id,post_votes,comment_id,comment_time,
#rn := CASE WHEN #prev_post_id = post_id THEN #rn + 1 ELSE 1 END AS rn,
#prev_post_id := post_id from
(select p.post_id,post_votes,comment_id,comment_time from
(SELECT post_id,post_votes from posts order by post_votes desc limit 10) p
left outer join
comments c on p.post_id=c.post_id order by post_id,comment_time desc )m
having rn<=5
SQL FIDDLE HERE (testing sample of retrieving 3 post order by desc and also 2 comments for each post respectively).
Related
I have article table
`id`,
`article_id`,
`stage_1_point`,
`stage_2_point`
I need top 10 articles based on
(stage_1_point+stage_2_point),
in the list and when i view any article i need to show its place.
My question is how can show its place without using order by.
USE ORDER BY (stage_1_point+stage_2_point) DESC
So, It will be like
SELECT `id`,
`article_id`,
`stage_1_point`,
`stage_2_point`
FROM YOUR_TABLE ORDER BY (stage_1_point+stage_2_point) DESC
LIMIT 0,10
UPDATE
As OP stated, he/she needs to know the position of specific article.
SELECT * FROM (SELECT `id`,
`article_id`,
`stage_1_point`,
`stage_2_point`,
#curRank := #curRank + 1 AS rank
FROM YOUR_TABLE, (SELECT #curRank := 0) r ORDER BY
(stage_1_point+stage_2_point) DESC) TAB
WHERE `article_id`=10
Above query will return rows for article_id 10, which will have a column Rank which tells the position of the article.
You can try below Query as below
Select
stage_1_point+stage_2_point as added_point,
columnsid,
article_id,
stage_1_point,
stage_2_point
FROM tablename
ORDER BY 1 desc
Limit 0,10
This will sort data based on column one result and fetch top 10 results only.
I have this structure (tables) of forum
I want to select last post (row from forum_post table) from category.
SQL so far:
SELECT * FROM table_post
WHERE topic_id = (SELECT MAX(id) FROM table_topic WHERE category_id = {$id})
ORDER BY id ASC LIMIT 1
Question: How to modify this select to achieve my goal?
Assuming that "last" means the biggest id, I would suggest order by and limit:
select fp.*
from forum_post fp join
forum_topic ft
on fp.topic_id = ft.id
where ft.category_id = $id
order by fp.id desc
limit 1;
I have tables tbl_posts and tbl_comments with primary keys post_id and comment_id respectively. I tried this code:
$allPosts=mysql_query("
select c.comment_id, post_id, count(*)
from post u,
comments c
where u.comment_id = c.comment_id
group by comment_id, post_id
LIMIT 10
");
but I have no clue what it does. How do I combine two tables so that the total comments determines the order of the listed posts from tbl_posts?
Try this, it's more readable if you separate per lines and work with joins
select c.comment_id, post_id, count(*)
from post u join comments c
on u.comment_id = c.comment_id
group by comment_id, post_id LIMIT 10
It looks like you have tables named tbl_comment and tbl_post but your query has them listed as just comment and post.
select c.comment_id, post_id, count(*)
from tbl_post u, tbl_comments c
where u.comment_id = c.comment_id
group by comment_id, post_id LIMIT 10
$allPosts=mysql_query("select c.comment_id, post_id, count(*) from tbl_post u, tbl_comments c where u.comment_id = c.comment_id group by comment_id, post_id LIMIT 10");
This just fixes the query so it runs, and does not address any content issues you may have, namely the group by on both (what I am guessing) are primary keys.
** EDIT **
To fix the sorting try:
SELECT tbl_post.comment_id, count(*)
FROM tbl_post, tbl_comments
WHERE tbl_post.comment_id = tbl_comment.comment_id
GROUP BY comment_id LIMIT 10
ORDER BY count(*)
Explanation of your SQL:
You are selecting column comment_id from table comments, column post_id from table post using a inner join, grouping by comment_id, post_id, with a limit of 10 results.
I would try:
$allPosts = mysql_query("SELECT * FROM
(SELECT c.comment_id, u.post_id, COUNT(*) AS 'count' FROM post u
LEFT JOIN comments c ON c.comment_id = u.comment_id
GROUP BY c.comment_id, u.post_id)
ORDER BY count DESC LIMIT 10");
I like to make an forum teaser for my website. Its easy to just show the latest posts or threads.. I like to get latest threads and posts in the same query, ordered by the last activity. So its going to be ordered by REPLY TO POST date, and THREAD POST date in the same query. I think it has some think to do with how you GROUP it, but I'm not sure.
Tables
threads
id, header, text, date, author
posts
id, text, date, author, thread_id
Example of usage
20 minutes ago - How to make an php/mysql script (2)
17 minutes ago - Pls help me out here (0)
1 hour ago - I need help with PHP (1)
As you see, both answered threads and new threads are on the list. (I need a date of the latest reply or when it was created, header and a count() of replys)
I hope you get, and know how to do this.
Troels
UPDATE:
I have this, and its okay, but i only get threads with replys.
SELECT
threads.*,
posts.*,
(SELECT date FROM posts WHERE thread_id = threads.id ORDER BY date DESC LIMIT 0,1) AS postdate,
(SELECT count(id) FROM threads WHERE thread_id = thread.id) AS replys
FROM
threads,
posts
WHERE
threads.id = posts.thread_id
GROUP BY
thread_id
ORDER BY
postdate DESC,
thread.date
LIMIT
0,15
HOW CAN I DO THIS?
UPDATE
aaaaaaaaaaaaaaaaawwwww Yeah!!!!
I managed to do it myself :-) Took a while to get it right.
SELECT
fisk_debat.id,
fisk_debat.dato,
IF((SELECT count(id) FROM fisk_debat_svar WHERE debatid = fisk_debat.id) < 1, fisk_debat.dato, (SELECT dato FROM fisk_debat_svar WHERE debatid = fisk_debat.id ORDER BY dato DESC LIMIT 0,1)) AS svardato,
fisk_debat.overskrift,
(
SELECT count(fisk_debat_svar.debatid)
FROM fisk_debat_svar
WHERE fisk_debat_svar.debatid = fisk_debat.id
) AS svar
FROM fisk_debat
GROUP BY id
UNION
SELECT
fisk_debat_svar.debatid AS id,
max(fisk_debat_svar.dato) AS dato,
max(fisk_debat_svar.dato) AS svardato,
(
SELECT fisk_debat.overskrift
FROM fisk_debat
WHERE fisk_debat.id = fisk_debat_svar.debatid
) AS overskrift,
(
SELECT count(fisk_debat_svar.debatid)
FROM fisk_debat_svar
WHERE fisk_debat_svar.debatid = id
) AS svar
FROM fisk_debat_svar
WHERE id != id
GROUP BY id
ORDER BY svardato DESC, dato DESC
LIMIT 0,15
If you want to keep the current DB structure, you'll need a Union to get the desired result.
An example can be found at http://www.mysqltutorial.org/sql-union-mysql.aspx
However, I'd still advise to change the structure as explained in the comments to your question.
WHERE clauses only select threads with replies, which is normal. You have to use the LEFT JOIN syntax.
Try this:
SELECT
threads.*,
posts.*,
(SELECT date FROM posts WHERE thread_id = threads.id ORDER BY date DESC LIMIT 0,1) AS postdate,
(SELECT count(id) FROM threads WHERE thread_id = thread.id) AS replys
FROM
threads
LEFT JOIN
posts
ON
threads.id = posts.thread_id
ORDER BY
postdate DESC,
thread.date
LIMIT
0,15
I want to list latest activity in my forum
UPDATE:
I got this to work now.
SELECT
fisk_debat.*, fisk_debat_svar.*,
(SELECT dato FROM fisk_debat_svar
WHERE debatid = fisk_debat.id
ORDER BY dato DESC LIMIT 0,1) AS svardato,
(SELECT count(id) FROM fisk_debat_svar
WHERE debatid = fisk_debat.id) AS svar
FROM
fisk_debat_svar, fisk_debat
WHERE
fisk_debat.id = fisk_debat_svar.debatid
GROUP BY
debatid
ORDER BY
svardato DESC, fisk_debat.dato
LIMIT
0,15
Now I want to list newly created threads from the forum too and it have to blend into the list like the others. Also ordered by date. Like to different queries merged together. I know UNION but its not the same columns.
Need help.
SELECT
post.id,
post.date,
post.header,
post.username,
COUNT(reply.postid) AS reply,
reply.date AS replydate
FROM
post AS post
INNER JOIN
reply ON post.id = reply.postid
GROUP BY postid
ORDER BY replydate DESC
LIMIT 0,15
Simply add the sorting order