Sorting MYSQL rows through a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Sorting MYSQL rows through a string

Post by Icethrill »

So I got a rows in a database that I need to sort by 4 strings. atm I am using this bit of code. I think it should work maybe its an easy error or you need a special function for sorting rows based on a string.

Code: Select all

$kategori1 = 'Politik';
                $query = 'SELECT * FROM url_list WHERE url_kategori = '$kategori1' ORDER BY url_id ASC';
Anyone got an idea how I can do this? Googled some what on this but found nothing.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Sorting MYSQL rows through a string

Post by Mark Baker »

$query = 'SELECT * FROM url_list WHERE url_kategori = '$kategori1' ORDER BY stringColumnName1 ASC, stringColumnName2 DESC, stringColumnName3 ASC, stringColumnName4 DESC';
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Re: Sorting MYSQL rows through a string

Post by Icethrill »

Mark Baker wrote:$query = 'SELECT * FROM url_list WHERE url_kategori = '$kategori1' ORDER BY stringColumnName1 ASC, stringColumnName2 DESC, stringColumnName3 ASC, stringColumnName4 DESC';
Thanks for the answer, it isnt exactly what I need. I am gonna make 4 different select statements. it is the column url_kategori I need to sort based on 4 strings that exists among the rows. Any idea if I explained it this way?
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Sorting MYSQL rows through a string

Post by mickeyunderscore »

You mean you need to group them by the field 'url_kategori', like to count how many there are per category?
Icethrill
Forum Newbie
Posts: 13
Joined: Fri Jan 02, 2009 9:55 am

Re: Sorting MYSQL rows through a string

Post by Icethrill »

mickeyunderscore wrote:You mean you need to group them by the field 'url_kategori', like to count how many there are per category?
exactly, thats what I need to make.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Sorting MYSQL rows through a string

Post by mickeyunderscore »

You need to use the SQL GROUP BY clause:

Code: Select all

 
SELECT COUNT( * ) AS Quantity, url_kategori
FROM url_list
GROUP BY url_kategori
 
Post Reply