Page 1 of 1

mysql query

Posted: Wed Mar 08, 2006 6:42 pm
by pedroz
I have the following table:

posts | replies
1 | 5
2 | 2
3 | 0
4 | 5
5 | 3
6 | 4
7 | 8
8 | 3
9 | 0
10 | 1

I would like to order the last 5 posts order by replies DESC

posts| replies
7 | 8
6 | 4
8 | 3
10 | 1
9 | 0

Could you provide my the query?
...

Posted: Wed Mar 08, 2006 7:27 pm
by d3ad1ysp0rk
Wow, you're so close to being there.. what's stopping you from completing the query?

Anywho,

Code: Select all

SELECT posts FROM tablename ORDER BY replies DESC

Posted: Wed Mar 08, 2006 7:35 pm
by pedroz

Code: Select all

SELECT posts FROM tablename ORDER BY replies DESC
I think this code will display all posts and I need the last 5 five posts

Posted: Wed Mar 08, 2006 7:55 pm
by d3ad1ysp0rk
Oh, I'm sorry, I didn't notice that part. :|

Code: Select all

SELECT posts FROM tablename ORDER BY replies DESC LIMIT 0,5

Posted: Thu Mar 09, 2006 11:41 am
by pedroz
It seems I have to execut a subquery but I am not receiving any echo... :(

What is wrong with the code above?

Code: Select all

$Table='daTable';
$discussions=5;
$fields='posts,replies';

$select="SELECT * FROM '.$Table.' ORDER BY post DESC LIMIT '.$discussions.'";

$cols=mysql_fetch_row(mysql_query('SELECT '.$fields.' FROM ('.$select.') ORDER BY replies DESC'));

Posted: Thu Mar 09, 2006 12:32 pm
by a94060
i dont think that the () is needed around the vars maybe?

Posted: Thu Mar 09, 2006 12:35 pm
by pedroz
1. First part I found the error

Code: Select all

$select='SELECT * FROM '.$Table.' ORDER BY post DESC LIMIT '.$discussions.'';
instead

Code: Select all

$select="SELECT * FROM '.$Table.' ORDER BY post DESC LIMIT '.$discussions.'";

2. Second error

Code: Select all

$cols=mysql_fetch_row(mysql_query('SELECT '.$fields.' FROM ('.$select.') dt ORDER BY replies DESC'));

But still not running well the query. Only showing me one result instead 5


Removing () it will not show any result...

Posted: Thu Mar 09, 2006 12:44 pm
by feyd
mysql_fetch_row() returns one record. You'll need to call it in a loop to get all the rows.