473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get records against root level id using MySql Query

245 New Member
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 2343
Rabbit
12,516 Recognized Expert Moderator MVP
Join the product table to the category table so you can filter on the parent id.
Dec 13 '11 #2
neovantage
245 New Member
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 Recognized Expert Moderator MVP
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 New Member
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 Recognized Expert Moderator MVP
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
1509
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 show up when using the print command as though they'd been entered but hadn't actually been written to the database. I'm hoping to find some sort of if/else script that can be easily used with 'insert into' in my PHP. Am using MySQL 4.1.11nt and...
2
1382
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
6635
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 length is 642 bytes ( I have appended it to the end of this message). I suspect that the reason may have something to do with an incorrect declaration of which class to de-serialize to. In the attached code I substituted @@@@@@@ in the code below with...
7
161296
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... ########################################### package MySQL-client-5.0.24a-0.glibc23 is already installed package MySQL-server-5.0.24a-0.glibc23 is already installed
2
2985
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 using mysql query browser, and i got the following error:
4
6490
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 retrieved using list/menu box. When i check only a single checkbox to insert the checked category ,selecting that category through list box gives out the entire data of the user corresponding to that category. However when i check multiple checkboxes and...
4
2749
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 query properties are 'no locks' and dynaset record type but just executing the query as a datasheet, locks some records in the original sql server table is this how it should work ? seems to me that it should not lock anything
1
4831
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 database tables. All works well, and I can do what I need except I am having some trouble with applying an MySQL query result into a defined variable. This would seem like such an easy thing to do. The only examples I can find out there are...
3
4898
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 type server host and port 3306 user name admin + password and default schema and I get access denied message. I use putty to connect to the server using IP and port 22 and it connects to the server.
5
9122
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 $conn = @mysql_connect($db_server,$db_user,$db_pass) or die("Connection to Database Server Failed"); @mysql_select_db($db) or die("Database Selection Failed"); return $conn; }
0
8145
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8095
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8556
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8410
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.