LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2004, 03:56 PM   #1
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Rep: Reputation: 30
Massive SQL Query


Might there have been an easier way to do this? I don't know if it's ugly or beatiful. I really can't believe that it worked. But it did.

$sql="
SELECT sub_cats.sub_cat_id, sub_cat_name, super_cat, cat_name, p_title
FROM products INNER JOIN (((categories
INNER JOIN cat_index ON categories.cat_id = cat_index.cat_id)
INNER JOIN sub_cats ON cat_index.sub_cat_id = sub_cats.sub_cat_id)
INNER JOIN sub_cat_index ON sub_cat_index.sub_cat_id = sub_cats.sub_cat_id)
ON products.p_id = sub_cat_index.p_id
WHERE products.p_id = $_GET[p_id]";

Actually it might not be that massive, but my head is still hurting from it. I am new to SQL and don't know if there are any shortcuts to this sort of thing or not.
 
Old 02-03-2004, 04:22 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It can probably be smaller, but I'd like to see more details about the database structure. Do you really need to join everything? You may not need to join with indexes and you can probbaly use NATURAL JOIN insted of your JOINs. But my opinion may change after seeing table structures.
 
Old 02-04-2004, 07:36 AM   #3
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Original Poster
Rep: Reputation: 30
Here is the structure of this part of the database:

products
product_index; associates products with sub_cats
sub_cats
sub_cat_index; associates sub_cat with categories
categories

The reason I used the indexes, which are actually associations, was that I wanted to be able to add each product to more than one sub_cat and add each sub_cat to more than one category. The reason I used the aforementioned query was to display "category->sub_cat->product" at the top of a product page. I could have done three seperate, simpler queries to pull each name individually but I would assume it would be better programming to run a single query to get all the information than querying the server 3 times.
 
Old 02-04-2004, 03:58 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Code:
SELECT sub_cats.sub_cat_id, sub_cat_name, super_cat, cat_name, p_title
FROM products 
NATURAL JOIN product_index
NATURAL JOIN sub_cats
NATURAL JOIN sub_cats_index
NATURAL JOIN categories
WHERE products.p_id = $_GET[p_id]";
So it looks that your query is the only one possible to do this with this database structure (above the same in a different way).
 
Old 02-04-2004, 08:42 PM   #5
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Original Poster
Rep: Reputation: 30
I didn't learn NATURAL JOIN in my rudimetary self study of SQL. How is different from INNER JOIN?
 
Old 02-05-2004, 04:49 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
When using NATURAL JOIN you don't specify ON. The tables are joined using column(s) from the tables having the same name. For example, there are categories.cat_id and cat_index.cat_id in your tables. Two columns have the same name: cat_id. When you NATURAL JOIN them, the tables will be joined using cat_id from both of them.
It's handy plus (more important) - it's usually faster.


Warning here: when using natural join always make sure you're joining using the columns you want to (is there's another pair of columns with the same name they will be also used).
 
Old 02-06-2004, 04:47 AM   #7
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
Mara, what's the behaviour of a natural join? Is it a left join, right join or something else?
 
Old 03-05-2004, 04:24 PM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Ups...you asked the quiestion a month ago. I'll answer now for future reference mostly, I guess.

Natural join is something different than left or right join. In fact, you can write 'left natural join' and so on. It makes the join operation use columns with the same name from both tables (and makes the operation faster because of this).
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL query performance smaida Programming 6 06-08-2005 09:22 AM
Massive SQL dump without local access. xconspirisist Linux - General 1 01-30-2005 10:07 AM
SQL query help pls. vickr1z Programming 8 10-18-2004 11:25 PM
Bizarre SQL select query acid_kewpie Programming 6 01-20-2004 12:47 PM
SQL Query question oulevon Programming 7 01-16-2004 01:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration