tibberous Posted December 16, 2007 Share Posted December 16, 2007 I have two tables, one named items and the other named categories. Categories has two columns, an id and a name. Ex: id name 1 Action 2 Classic 3 Adventure The items table has a field called category. This is a number, that represents the id in the other table. So, if the category value was 2, the category would be Classic. If I have an item in the items table, and want to get the text name of the category, how would I make the query? Basically I am doing a lookup table, but I forget how. Can anyone help me out? If I'm not being clear I'll explain better - it is kind of hard to say Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 16, 2007 Share Posted December 16, 2007 SELECT i.item, c.name FROM items i LEFT JOIN categories ON i.category = c.id Quote Link to comment Share on other sites More sharing options...
beebum Posted December 16, 2007 Share Posted December 16, 2007 You could also do: SELECT items.item as item, category.name as category FROM items, categories where items.category = category.id; I'm not sure but I think this only became available in 5.0. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 16, 2007 Share Posted December 16, 2007 You could also do: SELECT items.item as item, category.name as category FROM items, categories where items.category = category.id; I'm not sure but I think this only became available in 5.0. That method would work, but using joins is more efficient. Also, there is no reason to alias the field names as you did in that query. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.