2 Table MySql Query Assist

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
tjhopkinson
Forum Newbie
Posts: 1
Joined: Sun Jul 05, 2009 9:17 pm

2 Table MySql Query Assist

Post by tjhopkinson »

Kind gentlemen / ladies,

Here are the two tables I am working with ... the important columns at least --

Table #1 - items (catid -- which contains the ID# of whatever category it is from table #2)
Table #2 - categories (id,name)

I want to be able to display a search results page that allows me to sort in alphabetically ascending order all the pertinent information from Table #1 but based on the NAME column from Table #2.

So basically:

Select everything from items,categories order by categories name

Any assistance would be greatly appreciated, thanks.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: 2 Table MySql Query Assist

Post by califdon »

Code: Select all

SELECT * FROM [i]items[/i] JOIN [i]categories[/i] ON categories.id=items.id
   ORDER BY name
should do it.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: 2 Table MySql Query Assist

Post by jackpf »

Something like

Code: Select all

SELECT X.*, Y.`Name` AS `Name`
FROM tbl1 X
INNER JOIN tbl2 Y ON X.`Catid`=Y.`ID`
GROUP BY tbl2.`Catid`
ORDER BY `Name` ASC
If not, it's something like that.
Post Reply