Author Topic: Categorizing data  (Read 1960 times)

0 Members and 1 Guest are viewing this topic.

Offline -Felix-Topic starter

  • Enthusiast
  • Posts: 119
    • View Profile
Categorizing data
« on: October 04, 2005, 02:29:02 PM »
Lets say I have the details of 500 cars stored in mysql database.
What would be the best way to categorize these so that I can easily print a list of all the "blue cars" and all the cars with "airconditioning" for example?
Assuming that these are all in one table, the best way would probably to add a field that indicates what the color of the car is and if it has aircondition.

Ok, but what if one car has both qualities? What would be the best way to do this?

Offline neylitalo

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,970
  • Gender: Male
    • View Profile
    • The Netizen's Journal
Categorizing data
« Reply #1 on: October 04, 2005, 02:36:38 PM »
I would suggest adding a field for every possible "extra" in the car, and set it to 0 or 1 depending on if it has the feature.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Categorizing data
« Reply #2 on: October 04, 2005, 02:38:37 PM »
Code: [Select]
cars
====
id
make
model

features
========
id
name

cars_features_xref
==================
car_id
feature_id
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline -Felix-Topic starter

  • Enthusiast
  • Posts: 119
    • View Profile
Categorizing data
« Reply #3 on: October 04, 2005, 03:02:45 PM »
umm...thanks for the replies guys, but you lost me lol
...Im using a web based admin tool to store mysql data.
I guess Im really a beginner. could you explain a little more specifically what I should do? I can add a field, and then choose a type for it, default values etc., but I dont know what type would be best in this case when same car can have multiple qualities.

Offline ryanlwh

  • Staff Alumni
  • Addict
  • *
  • Posts: 1,924
    • View Profile
Categorizing data
« Reply #4 on: October 04, 2005, 03:10:46 PM »
the easiest way to go would be adding a field for each feature like neylitalo suggested:

Code: [Select]
table Car
============
id
manufacturer
make
year
aircondition (1 or 0, use tinyint or enum)
color (use enum or make it varchar)
doors (integer)

A more dynamic way is suggested by the above poster... but maybe too complex for a beginner...
« Last Edit: October 04, 2005, 03:11:29 PM by ryanlwh »
Please use EDIT * 100...
Please use
Code: [Select]
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Categorizing data
« Reply #5 on: October 04, 2005, 03:27:35 PM »
cars
====
id
make
model

example:

Code: [Select]
1 volkswagen jetta
2 volkswagen passat

features
========
id
name

example:

Code: [Select]
1 a/c
2 leather seats
3 sun roof

cars_features_xref
==================
car_id
feature_id

example:

Code: [Select]
1 1 (this means the jetta has a/c)
1 3 (this means the jetta (also) has a sun roof)
2 1 (this means the passat has a/c)
2 2 (this means the passat (also) has leather seats)

SQL: (i think this is right; not tested)

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']select[/span] c.id, c.make, c.model, f.name from cars c, features f, cars_features_xref x where c.id=x.car_id and x.feature_id=f.id [!--sql2--][/div][!--sql3--]
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/