Page 1 of 1
Sorting MYSQL rows through a string
Posted: Tue Feb 03, 2009 8:01 am
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.
Re: Sorting MYSQL rows through a string
Posted: Tue Feb 03, 2009 8:05 am
by Mark Baker
$query = 'SELECT * FROM url_list WHERE url_kategori = '$kategori1' ORDER BY stringColumnName1 ASC, stringColumnName2 DESC, stringColumnName3 ASC, stringColumnName4 DESC';
Re: Sorting MYSQL rows through a string
Posted: Tue Feb 03, 2009 8:12 am
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?
Re: Sorting MYSQL rows through a string
Posted: Tue Feb 03, 2009 8:15 am
by mickeyunderscore
You mean you need to group them by the field 'url_kategori', like to count how many there are per category?
Re: Sorting MYSQL rows through a string
Posted: Tue Feb 03, 2009 8:16 am
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.
Re: Sorting MYSQL rows through a string
Posted: Tue Feb 03, 2009 8:18 am
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