473,396 Members | 1,814 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,396 software developers and data experts.

Help with an SQL query please

Hi there, first request for help, an a newbee so be gentle with me !
I have :

Two tables

products & categories

products table -
productID
productcategoryID
productname

categories table -
categoryID
name
parentID

I want a query which selects each product shows it's name, it's product ID, it's product category (by name) and it's product category parent (by name). My problem is that within categories the parentID can be the categoryID and I'm not sure how to construct the Query

Example data

productID - 1001
productcategoryID - 120
productname - "200gb Hard Disk"

productID - 1002
productcategoryID - 110
productname - "R/W CD Drive"

categoryID - 100
name - Drives
parentID - Null

categoryID - 110
name - CD Drives
parentID - 100

categoryID - 120
name - Hard Disk Drives
parentID - 100

The output I would like is

1001 200gb Hard Disk Hard Disk Drives Drives
1002 R/W C/D Drive CD Drives Drives

I would appreciate your assistance

Kind Regards

Darrin
Aug 22 '06 #1
2 1535
masdi2t
37
try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     p.productID as ProductID,
  3.     p.productname as ProductName,
  4.     c.name as ProductCategory,
  5.     pc.name as ParentProductCategory
  6. FROM products p
  7. LEFT JOIN categories c on p.productcategoryID = c.categoryID
  8. LEFT JOIN categories pc on c.parentId = pc.categoryID
  9.  
Aug 23 '06 #2
Hiya

Thanks for responding to my question about an SQL Select statement, unfortunately I wasn't specific about the actual table and field names that I am using and I am having trouble transposing the instruction you left into my DB. I have listed below the actual table names and fields, could you be kidn enough to resubmit detals using the table/field names I have shown below

Many thanks

Darrin

TABLE - ss_categories
Fields - categoryID, name, parent
TABLE ss_products
Fields - productID, categoryID, description, name, price, in_stock

I want to be able to SELECT product information (name, description etc) and show it's category name and that categories parent as well
Aug 23 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
4
by: sah | last post by:
I need some help with the following query: DECLARE @SRV VARCHAR(20), @date smalldatetime SET @SRV = (select @@servername) SET @date = '20040901' select Srv_Name = @SRV, DB_Name = 'DB_NAME',...
6
by: Martin Hampl | last post by:
Hi, I am using PostgreSQL 7.4, but I did have the same problem with the last version. I indexed the column word (defined as varchar(64)) using lower(word). If I use the following query,...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
6
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the Lotus product). There are things I could easily...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
5
by: Rated R1 | last post by:
I wrote this before in the NGs, so I am going to paste the responses that I got and see if someone can please help me. Email me and we can set something up as Id even be willing to pay for your...
1
by: David | last post by:
Hi, I cannot get the following (MS Access) SQL statement working in my asp page, please can anyone help me ? Thanks :-) ------------------------------------------------ <% strQuery =...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
0
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.