Old 23-05-2007, 15:27   #1 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
SQL sorting

I want to do a query on items but also have groups listed and sorted by name. My tables are:
Code:
groups --------- id name items --------- id name group_id
and I have an sql statement like:
Code:
SELECT `items`.`id`,`items`.`name` FROM `items` LEFT JOIN `groups` ON `items`.`group_id`=`groups`.`id` ORDER BY `name`LIMIT 10,10
but that doesn't work. what I need is to have groups.name and items.name combined and sorted alphabetically.

I can think of one way to do it (dumping the results to a temporary table then sort in another select) but am wondering which is the better or if there is a another way.
__________________
Quote:
"Why reinvent the wheel"? Simply because it's not round enough and I don't like the treads.
  Reply With Quote
Old 23-05-2007, 15:36   #2 (permalink)
gray
i still want paying
 
gray's Avatar
 
Join Date: Oct 2003
Location: newcastle, uk
Posts: 4,728
off the top of my head, without reading fully into what you want, and after a few mojitos (this is gonna be helpful then)

have you tried GROUP BY?
  Reply With Quote
Old 24-05-2007, 08:02   #3 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
I don't think GROUP BY is an option. An Item may not be part of a group leaving its group field as 0. The GROUP BY option would combine all these items would it not?
__________________
Quote:
"Why reinvent the wheel"? Simply because it's not round enough and I don't like the treads.
  Reply With Quote
Old 31-05-2007, 06:06   #4 (permalink)
LazyMoon
Registered
 
LazyMoon's Avatar
 
Join Date: Sep 2006
Location: Adelaide, Australia
Posts: 12
I'm not too sure what you actually want from your SQL. What do you mean by "what I need is to have groups.name and items.name combined and sorted alphabetically"?

Can you give us an example of what you want.

If your trying to sort multiple columns you can try:

Code:
SELECT `items`.`id`,`items`.`name` FROM `items` LEFT JOIN `groups` ON `items`.`group_id`=`groups`.`id` ORDER BY `items`.`group_id` ASC, `items`.`name` ASC LIMIT 10,10
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8