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

How to get records against root level id using MySql Query

245 100+
Hi,
I have a scenario in which i am stuck with in my custom shopping cart.

I have tables of category and product.Each product is against some category. for example


Category Table
Fields are catid, parentid, catname

Data
catid = 1, parentid = 0, catname = Electroics
catid = 2, parentid = 1, catname = Computers
catid = 3, parentid = 2, catname = Sony
catid = 4, parentid = 2, catname = HP

Product Table
Fields are prodid, catid, prodname

Data
prodid = 1, catid = 3, prodname = VAIO Laptops
prodid = 2, catid = 3, prodname = VAIO Desktop
prodid = 3, catid = 3, prodname = VAIO Signature Collection
prodid = 4, catid = 4, prodname = HP Laptops
prodid = 5, catid = 4, prodname = HP Desktop

Now what i want that when some one select category computers then it will get all the records which are either Sony Products or HP Products. I want to write MySQL query but unable to write. Can come one guide me how can i write query for this...?
Dec 13 '11 #1
5 2340
Rabbit
12,516 Expert Mod 8TB
Join the product table to the category table so you can filter on the parent id.
Dec 13 '11 #2
neovantage
245 100+
Yes but it gives me 1 to 1 result.
I mean look at my format of query
select * from category c, product p where c.catid=p.catid and p.catid={posted-catid}

Now if i select sony then it will give me the products which relates to sony and if i select hp then it will give me the products which relates to hp but when i will click on Electronics, It does not return any record as Electronic id is not there in the product table & here is my point where i stuck. What i want is that when i click on Electronics then it shows me all the products which comes under Electronic child categories. So please help me out to sort out my problem
Dec 13 '11 #3
Rabbit
12,516 Expert Mod 8TB
Because of the data structure you've chosen, you need as many joins as you have levels of categories. If you need a dynamic hierarchy, I suggest you switch over to a preorder tree structure. That will allow you to have as many levels of hierarchy as you want without increasing the amount of joins needed.

Either way, once you have all the joins needed for the full path down to the child node, you can filter on any chosen level. With your current data structure where you have a three-level hierarchy, you need to join the category table three times to get the data needed for each level. Then you can filter on any of those levels.
Dec 13 '11 #4
neovantage
245 100+
Can you guide me the query format as i don't have that level expertise to write query. That will be really helpful for me.
Dec 14 '11 #5
Rabbit
12,516 Expert Mod 8TB
Give it a shot and post your resultant SQL and we will help you through the specifics. The key part is to join the category table 3 times to get your 3-level hierarchy.
Dec 14 '11 #6

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

Similar topics

5
by: cover | last post by:
Is there any basic, sure fire 'if - else' script that will ensure that records entered into my form will actually be written to the database? I noticed when debugging one day that records would...
2
by: Chris W | last post by:
I have these 2 queries. SELECT count(*) gifts FROM gift g WHERE g.this and g.that SELECT count(*) events FROM events e WHERE e.this and e.the other thing
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
7
by: rajbala.3399 | last post by:
Hi , I want to download sql in my linux system........... # rpm -ivh MySQL-server-5.0.24a-0.glibc23.i386.rpm MySQL-cl ient-5.0.24a-0.glibc23.i386.rpm Preparing... ...
2
by: stmfc | last post by:
i have a problem with user creation for mysql db i think the problem stems from string style. GRANT ALL ON newDB.* TO 'test'@'localhost' IDENTIFIED BY 'test'; i am...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
4
by: Roger | last post by:
on sql 2005, I've got a view with select permission granted, the view just "select * from table" using odbc in access97, I linked this view and I create a query to retrieve certain fields the...
1
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
3
by: emsik1001 | last post by:
Hi http://dev.mysql.com/doc/query-browser/en/mysql-query-browser-connection.html I'm trying to connect from my Windows based PC to a dedicated server which is running on Linux system. I...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.