Skip to main content
gamedev.net
🔒 Locked

SQL help?

Started by Toolmaker Jul 3, 2004 at 5:18 AM 1 replies 650+ views
Original Post
Toolmaker
Toolmaker
I seem to suck at SQL, perhaps I should read some tutorials again. However, that doesn't get me much further for now, so perhaps someone else here can help me. I have 2 tables, MenuCat and Menu.

MenuCat:
ID     Prim key, int, auto_increment
CatType  varchar(30), not null

Menu:
MenuID prim key, int, auto_increment
CatID  int, unsigned, null  (Used by parent item)
PID    int, null    (Linked to MenuID by child item)
Name   varchar(20), not null
URL    varchar(255), not null
Now, I want to create my menu from this in PHP. The PHP is the easy part, but I can't seem to figure out how to form my query to grab my menu. I want to know the CatType, menuid, name, etc. I used a recursive menu table to keep it less complicated then having 2 menu tables(A Parent and child table). I tried this query, but MySQL chokes on it

SELECT * 
FROM Menu
WHERE Menu.MenuID
IN (
SELECT DISTINCT Menu.PID
FROM Menu
WHERE Menu.PID IS NOT NULL 
)
GROUP BY MenuID
LIMIT 0 , 30 
Any help?
 
evolutional
evolutional
Look at JOINS

SELECT * FROM Menu INNER JOIN MenuCat ON Menu.CatID = MenuCat.ID;


Or you could go...

SELECT * FROM Menu, MenuCat WHERE Menu.CatID = MenuCat.ID;

Have a look at this link for details why this works.

Hope this helped!
Toolmaker
Toolmaker
I got another query, to extract some other data, and I want to merge both queries, but I'm unable to do so, anyone has an idea?

SELECT Menu.MenuID AS MenuID, ParentMenu.CatID, Menus.Name, Menu.URL, ParentMenu.Name AS ParentName, ParentMenu.URL AS ParentURL, ParentMenu.MenuID AS ParentID, '0' AS IsParentFROM (Menu AS ParentMenuINNER JOIN Menu ON ParentMenu.MenuID = Menu.PID)


That's my other query, how can I merge them?

Toolmaker
 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.