To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-06-2003, 07:13 PM   #1
Mark_78
Junior Member
 
Join Date: Dec 2003
Posts: 4
mySQL queries ?

What do you think to these mySQL queries ? could they be made better ?
Code:
SELECT cats.*, forums.*, members.MID, members.Username, topics.Title 
FROM pmb_categories as cats, pmb_forums as forums 
LEFT JOIN pmb_members as members 
ON (members.MID = forums.Last_Poster_ID ) 
LEFT JOIN pmb_topics as topics 
ON ( topics.TID = forums.Last_Topic_ID ) 
WHERE cats.CID = forums.CID ORDER BY cats.Position, forums.Position
Code:
SELECT stats.*, members.MID, members.Username 
FROM pmb_stats as stats, pmb_members as members 
WHERE stats.New_Member_ID = members.MID
[mod] Edited to fit the screen better... please try not to stretch out the screen. It makes it harder to read.

Last edited by BuzzLY; 12-06-2003 at 08:12 PM.
Mark_78 is offline   Reply With Quote
Old 12-06-2003, 08:21 PM   #2
BuzzLY
2($infinity) && $beyond
 
BuzzLY's Avatar
 
Join Date: Nov 2002
Location: Star Command
Posts: 2,535
When using any JOINS, in order to clear up potential confusion, you should JOIN all of your tables. The rule of thumb is that when using joins, ON statements define the links between primary and foreign keys, and WHERE statements define restrictions (such as "WHERE lastname = 'Smith'").

Therefore, I would write your first query as such:
Code:
SELECT cats.*, forums.*, members.MID, members.Username, topics.Title 
FROM pmb_categories as cats
JOIN pmb_forums as forums 
ON cats.CID = forums.CID 
LEFT JOIN pmb_members as members 
ON members.MID = forums.Last_Poster_ID
LEFT JOIN pmb_topics as topics 
ON topics.TID = forums.Last_Topic_ID
ORDER BY cats.Position, forums.Position
Notice in this case you are simply linking tables, and not limiting your resulting data to any subset -- there is no need for a WHERE clause. If you add a WHERE clause, it's simply to narrow down your dataset, such as "WHERE cats.name = 'general'"
__________________
New to the board? Check out the guidelines
| Color Picker | Blogification |
¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
With all its sham, drudgery, and broken dreams, it's still a beautiful world.
BuzzLY is offline   Reply With Quote
Old 12-07-2003, 07:00 AM   #3
Mark_78
Junior Member
 
Join Date: Dec 2003
Posts: 4
Thanks, , and sorry about the my first post!
Mark_78 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 01:27 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.