473,287 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Query Multiple Tables for Categories & Products

TASK:
I have to generate a report with all categories, subcategories and
products in database.

PROBLEM:
I want to write one query that will return:

1. category
2. subcategory: determined by parent_id
3. products in each category or subcategory.

I have four tables with the following structure:

------------------------------------------------------------------------
---

CREATE TABLE `categories` (
`categories_id` int(11) NOT NULL auto_increment,
`parent_id` int(11) NOT NULL default '0',
`sort_order` int(3) default NULL,
PRIMARY KEY (`categories_id`),
)

INSERT INTO `categories` VALUES (1, 0, 1);
INSERT INTO `categories` VALUES (2, 1, 1);
INSERT INTO `categories` VALUES (3, 0, 2);
INSERT INTO `categories` VALUES (4, 3, 1);

CREATE TABLE `categories_description` (
`categories_id` int(11) NOT NULL default '0',
`categories_name` varchar(32) NOT NULL default '',
PRIMARY KEY (`categories_id`),
)

INSERT INTO `categories_description` VALUES (1, 'Sedans');
INSERT INTO `categories_description` VALUES (2, 'Honda');
INSERT INTO `categories_description` VALUES (3, 'Luxury');
INSERT INTO `categories_description` VALUES (4, 'Mercedez');

CREATE TABLE `products_to_categories` (
`products_id` int(11) NOT NULL default '0',
`categories_id` int(11) NOT NULL default '0',
PRIMARY KEY (`products_id`,`categories_id`)
)

INSERT INTO `products_to_categories` VALUES (100, 1);
INSERT INTO `products_to_categories` VALUES (101, 2);
INSERT INTO `products_to_categories` VALUES (102, 4);
INSERT INTO `products_to_categories` VALUES (103, 4);

CREATE TABLE `products` (
`products_id` int(11) NOT NULL auto_increment,
`products_model` varchar(12) default NULL,
`products_image` varchar(64) default 'image_na.gif',
`products_price` decimal(15,4) NOT NULL default '0.0000',
PRIMARY KEY (`products_id`),
)

INSERT INTO `products` VALUES (100, 'Civic', 'civic.jpg', '150000');
INSERT INTO `products` VALUES (101, 'Accord', 'accord.jpg', '250000');
INSERT INTO `products` VALUES (102, 'S500', 's500.jpg', '60000');
INSERT INTO `products` VALUES (103, 'S600', 's600.jpg', '90000');

------------------------------------------------------------------------
---

I can do this with multiple queries, but to be more efficient can I do
this with one query?

Your help is appreciated.

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/my***********...ie.nctu.edu.tw

Jul 19 '05 #1
0 3207

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Erik Thorsen | last post by:
I need help with a tricky questions which I belive have a simple solution? For those who know Nwind.mdb, the example database that comes with microsoft access, it has several different tables. One...
2
by: Colleyville Alan | last post by:
I want to extract some records that are common to three tables, but not contained in the fourth. Following what I have see in the archives and also trying the unmatched records query wizard, if I...
4
by: Steve Klett | last post by:
(I posted this in ADO group, but I think this group will be better) Hi- I need to develop an FAQ section for our website. We would like to break up the FAQ by products, then categories with...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
2
by: DarrinM | last post by:
Hi there, first request for help, an a newbee so be gentle with me ! I have : Two tables products & categories products table - productID productcategoryID
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
5
by: john7 | last post by:
I have three tables one for categories, one for products1 and another for products2. I am using PHP and MySQL. so for example I categories categoryid name 1 programming 2 databases 3 ...
3
by: cmrhema | last post by:
Hi, I need help in phrasing this simple query. I have two tables 1. Category table, it consists of categoryid and productid 2. product table, it consists of categoryid, productid, productname,...
4
by: imaginit | last post by:
I need to build a query that queries products based on multiple categories. A product can have multiple categories attached to it and I want to query products that are only in ALL of the selected...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.