473,473 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Create 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 2690
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, "elektrongyorsito 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.comhttp://www.dbmonster.com/Uwe/Forums.aspx/mysql-general/200702/1

Feb 2 '07 #2
On Feb 2, 7:27 am, "elektrongyorsito 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.comhttp://www.dbmonster.com/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
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...
2
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...
1
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...
0
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...
3
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...
0
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,...
1
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...
13
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...
4
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...
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
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.