473,804 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Components and categories

Hello,
I have 3 tables:

T_component =TCO
cmp_id | name
1 | door
2 | exhaust

T_cmp_cat =TCC (categories and components)
id | cmp_id | cat_id
1 | 1 | 1
2 | 1 | 3
3 | 1 | 4
4 | 2 | 1
5 | 2 | 2

T_categories =TCA
cat_id | cat_name
1 | car
2 | motorcycle
3 | train
4 | airplane
5 | other
If I ask this:
select name,cat_name from TCO inner join TCC on TCO.cmp_id=TCC. cmp_id inner
join TCA on TCA.cat_id=TCC. cat_id

then I get, like this:

name | cat_name
door | car
door | train
door | airplane
exhaust |car
exhaust |motorcycle

But, I would like to get this:

Name | cat_car | cat_motorcycle | cat_train | cat_airplane | cat_other
door | 1 | 0 | 1 | 1 | 0
exhaust | 1 | 1 | 0 | 0 | 0

Is there any simple way to get?

When I created the db, I asked more poeple about this, and they suggested
this form, because flexible expanding of the categories. But now I think I
could work easier if I store the categories into the component table, like
this:

T_component =TCO
cmp_id | name | cat1 | cat2 ...
1 | door | 0 | 1 |...
2 | exhaust | 1 | 1 |...

But it displease me... How can I solve it correctly? Thank you

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....neral/200702/1

Feb 2 '07 #1
2 2709
What version of MySQL are you using? I'm asking because the only way
I can think of solving this is with an embedded SELECT statement....
which earlier versions of MySQL did not know how to handle.

Here is a general mockup of what I think may work for you:
SELECT name, (SELECT cat_id FROM T_cmp as b WHERE b.cmp_id = a.cmp_id
AND b.cat_id = 1) AS cat_car, etc...
FROM T_component AS a

I haven't tested this at all... it's just an idea.

On Feb 2, 2:27 am, "elektrongyorsi to via DBMonster.com" <u5089@uwe>
wrote:
Hello,

I have 3 tables:

T_component =TCO
cmp_id | name
1 | door
2 | exhaust

T_cmp_cat =TCC (categories and components)
id | cmp_id | cat_id
1 | 1 | 1
2 | 1 | 3
3 | 1 | 4
4 | 2 | 1
5 | 2 | 2

T_categories =TCA
cat_id | cat_name
1 | car
2 | motorcycle
3 | train
4 | airplane
5 | other

If I ask this:
select name,cat_name from TCO inner join TCC on TCO.cmp_id=TCC. cmp_id inner
join TCA on TCA.cat_id=TCC. cat_id

then I get, like this:

name | cat_name
door | car
door | train
door | airplane
exhaust |car
exhaust |motorcycle

But, I would like to get this:

Name | cat_car | cat_motorcycle | cat_train | cat_airplane | cat_other
door | 1 | 0 | 1 | 1 | 0
exhaust | 1 | 1 | 0 | 0 | 0

Is there any simple way to get?

When I created the db, I asked more poeple about this, and they suggested
this form, because flexible expanding of the categories. But now I think I
could work easier if I store the categories into the component table, like
this:

T_component =TCO
cmp_id | name | cat1 | cat2 ...
1 | door | 0 | 1 |...
2 | exhaust | 1 | 1 |...

But it displease me... How can I solve it correctly? Thank you

--
Message posted via DBMonster.comht tp://www.dbmonster.c om/Uwe/Forums.aspx/mysql-general/200702/1

Feb 2 '07 #2
On Feb 2, 7:27 am, "elektrongyorsi to via DBMonster.com" <u5089@uwe>
wrote:
Hello,

I have 3 tables:

T_component =TCO
cmp_id | name
1 | door
2 | exhaust

T_cmp_cat =TCC (categories and components)
id | cmp_id | cat_id
1 | 1 | 1
2 | 1 | 3
3 | 1 | 4
4 | 2 | 1
5 | 2 | 2

T_categories =TCA
cat_id | cat_name
1 | car
2 | motorcycle
3 | train
4 | airplane
5 | other

If I ask this:
select name,cat_name from TCO inner join TCC on TCO.cmp_id=TCC. cmp_id inner
join TCA on TCA.cat_id=TCC. cat_id

then I get, like this:

name | cat_name
door | car
door | train
door | airplane
exhaust |car
exhaust |motorcycle

But, I would like to get this:

Name | cat_car | cat_motorcycle | cat_train | cat_airplane | cat_other
door | 1 | 0 | 1 | 1 | 0
exhaust | 1 | 1 | 0 | 0 | 0

Is there any simple way to get?

When I created the db, I asked more poeple about this, and they suggested
this form, because flexible expanding of the categories. But now I think I
could work easier if I store the categories into the component table, like
this:

T_component =TCO
cmp_id | name | cat1 | cat2 ...
1 | door | 0 | 1 |...
2 | exhaust | 1 | 1 |...

But it displease me... How can I solve it correctly? Thank you

--
Message posted via DBMonster.comht tp://www.dbmonster.c om/Uwe/Forums.aspx/mysql-general/200702/1
If this is as complicated as it gets, then a query like the one below
should work. However, you may want to investigate pivot tables and
stored procedures, or alternatively, finding a php-based solution.

SELECT name, count( A.cat_id ) car, count( B.cat_id ) motorcyle,
count( C.cat_id ) train, count( D.cat_id ) airplane, count( E.cat_id )
other
FROM TCO
LEFT JOIN TCC A ON A.cmp_id = TCO.cmp_id
AND A.cat_id =1
LEFT JOIN TCC B ON B.cmp_id = TCO.cmp_id
AND B.cat_id =2
LEFT JOIN TCC C ON C.cmp_id = TCO.cmp_id
AND C.cat_id =3
LEFT JOIN TCC D ON D.cmp_id = TCO.cmp_id
AND D.cat_id =4
LEFT JOIN TCC E ON E.cmp_id = TCO.cmp_id
AND E.cat_id =5
GROUP BY 1;

Feb 4 '07 #3

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

Similar topics

6
18292
by: Brian | last post by:
Greetings, I'm working on a project that involved that has need of a categorization system. Logically speaking, the system will have elements, these elements will belong to at least a single category, sometimes more, additionally, categories may also be members of any number of categories. Well -- Thats the vision at least. I'm looking for some pointers, tutorials, white paper, napkins, whatever explaining how this can be done.
2
3529
by: Troy Lynch | last post by:
I'm working on writing a website which I need to have lists of products listed in categories and subcategories, and need to keep track of whats in the tree. Like how many products from the root all the way down through the subcategories. I was thinking maybe in each category it would have a field which lists all the root categories. I hope this is a good explanation... it's kinda hard to explain I guess. I figure there were probably some...
1
1413
by: Haqim al-Khayyami | last post by:
I know this sounds silly, but I've been playing with ASP for about 3 months, and it just now, today, clicked that I could be using active x stuff with my asp pages. The stuff I make falls into two categories, stuff for inhouse, and stuff that's on brinkster. How can I tell what active x components I can use, and find some docs on them?
0
3261
by: Ralph Guzman | last post by:
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.
3
1443
by: Edward Diener | last post by:
I am creating a component and I want one of my properties to be an embedded class with its own properties. When the component designer shows this property I want it to be able to expand this property in-line to enable the end-user to set its values. While my component is derived from System::ComponentModel::Component, my embedded class has to be derived from something else in order for it to be shown as an embedded class but be...
0
290
by: alt | last post by:
We would like to introduce a project LabDotNet.Com. http://www.labdotnet.com It is collection of .NET related components and tools. On this site you can read brief descriptions of the products, find out how much is it, where download the product , who developed the product and how contact with them. All products are sorted by categories. Here you find a board where visitors could discuss .NET Components with each other and with...
1
1571
by: pimpim32 | last post by:
Hi everybody. I want to add an Communication control on my dialog based project in VC2005. In VC6.0 I was using Project->Add to project->Add Components and controls etc In 2005 under the project menu I do not have Add to project :( When I created new MFC project I selected Dialog based and made sure ActiveX control radio was checked. Under Tools->Customize (Commands tab) I have Rearrange Commands->(Menu bar checked) select Project pressed...
13
3883
by: hornedw | last post by:
I have been working on a ecommerce website for myself. What I needed some assistance on was when i was trying to display the categories/subcategories for the different products. I decided to use the modified preorder tree transversal algorithm. What I wanted was on the first page is to display the catogories as follows Books (35) Electronics(23) The number inside the parenthesis being the number of products in that
4
2514
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for cross-posting. I am trying to build a "checklist", where a user can navigate to an ASP page on the intranet which shows a list of "questions" that the user can check off. I am trying to figure out how to do this so that it is scalable, but I am...
0
9716
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
9595
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
10604
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...
1
10359
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
10101
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
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
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.