Wohlgemerkt, dies ist eine sehr vage Frage, so versuche ich, mein Bestes zu beantworten :)
Hinweis: Es gibt viele bessere Möglichkeiten, dies zu tun, und ich könnte fragen, warum Sie tun es auf diese Weise, aber die folgende Methode sollte erreichen, wonach Sie suchen!
Hier gibt es 3 Tabellen:
CREATE TABLE `object` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`category` INTEGER NOT NULL ,
`name` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`)
);
CREATE TABLE `properties` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`objectid` INTEGER NOT NULL ,
`property_key` VARCHAR(100) NOT NULL ,
`property_value` MEDIUMTEXT NOT NULL ,
PRIMARY KEY (`id`)
);
CREATE TABLE `category` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`)
);
Jetzt können einige Daten in ihnen so tun hinzufügen:
INSERT INTO category VALUES ("","CAR");
INSERT INTO category VALUES ("","REALESTATE");
INSERT INTO category VALUES ("","ELECTRONIC");
INSERT INTO object VALUES ("",1,"98 CAMERO");
INSERT INTO object VALUES ("",1,"06 PRIUS");
INSERT INTO object VALUES ("",2,"A House Someplace");
INSERT INTO object VALUES ("",3,"iPod Touch");
INSERT INTO object VALUES ("",3,"iPhone");
INSERT INTO object VALUES ("",3,"Zune");
INSERT INTO properties VALUES ("",1,"color","red");
INSERT INTO properties VALUES ("",1,"doors","4");
INSERT INTO properties VALUES ("",1,"engine","v6");
INSERT INTO properties VALUES ("",2,"engine","electric");
INSERT INTO properties VALUES ("",2,"doors","4");
INSERT INTO properties VALUES ("",2,"color","blue");
INSERT INTO properties VALUES ("",6,"video-playback","true");
INSERT INTO properties VALUES ("",4,"video-playback","true");
INSERT INTO properties VALUES ("",5,"video-playback","true");
INSERT INTO properties VALUES ("",6,"manufacturer","microsoft");
INSERT INTO properties VALUES ("",5,"manufacturer","apple");
INSERT INTO properties VALUES ("",4,"manufacturer","apple");
Nun, hier ist das, was unsere Daten aussehen sollte.
mysql> select * from object;
+----+----------+-------------------+
| id | category | name |
+----+----------+-------------------+
| 1 | 1 | 98 CAMERO |
| 2 | 1 | 06 PRIUS |
| 3 | 2 | A House Someplace |
| 4 | 3 | iPod Touch |
| 5 | 3 | iPhone |
| 6 | 3 | Zune |
+----+----------+-------------------+
6 rows in set (0.00 sec)
mysql> select * from category;
+----+-------------+
| id | name |
+----+-------------+
| 1 | CAR |
| 2 | REALESTATE |
| 3 | ELECTRONICS |
+----+-------------+
3 rows in set (0.00 sec)
mysql> select * from properties;
+----+----------+----------------+----------------+
| id | objectid | property_key | property_value |
+----+----------+----------------+----------------+
| 1 | 1 | color | red |
| 2 | 1 | doors | 4 |
| 3 | 1 | engine | v6 |
| 4 | 2 | engine | electric |
| 5 | 2 | doors | 4 |
| 6 | 2 | color | blue |
| 7 | 6 | video-playback | true |
| 8 | 4 | video-playback | true |
| 9 | 5 | video-playback | true |
| 10 | 6 | manufacturer | microsoft |
| 11 | 5 | manufacturer | apple |
| 12 | 4 | manufacturer | apple |
+----+----------+----------------+----------------+
12 rows in set (0.00 sec)
Sie können eine unbegrenzte Anzahl von Kategorien, eine unbegrenzte Anzahl von Eigenschaften und eine unbegrenzte Anzahl von Objekten alle in diese Tabellen hinzufügen.
Jetzt sie verbinden alle zusammen:
mysql> SELECT * FROM object
-> LEFT JOIN category ON object.category = category.id
-> LEFT JOIN properties ON properties.objectid = object.id;
+----+----------+-------------------+------+-------------+------+----------+----------------+----------------+
| id | category | name | id | name | id | objectid | property_key | property_value |
+----+----------+-------------------+------+-------------+------+----------+----------------+----------------+
| 1 | 1 | 98 CAMERO | 1 | CAR | 1 | 1 | color | red |
| 1 | 1 | 98 CAMERO | 1 | CAR | 2 | 1 | doors | 4 |
| 1 | 1 | 98 CAMERO | 1 | CAR | 3 | 1 | engine | v6 |
| 2 | 1 | 06 PRIUS | 1 | CAR | 4 | 2 | engine | electric |
| 2 | 1 | 06 PRIUS | 1 | CAR | 5 | 2 | doors | 4 |
| 2 | 1 | 06 PRIUS | 1 | CAR | 6 | 2 | color | blue |
| 3 | 2 | A House Someplace | 2 | REALESTATE | NULL | NULL | NULL | NULL |
| 4 | 3 | iPod Touch | 3 | ELECTRONICS | 8 | 4 | video-playback | true |
| 4 | 3 | iPod Touch | 3 | ELECTRONICS | 12 | 4 | manufacturer | apple |
| 5 | 3 | iPhone | 3 | ELECTRONICS | 9 | 5 | video-playback | true |
| 5 | 3 | iPhone | 3 | ELECTRONICS | 11 | 5 | manufacturer | apple |
| 6 | 3 | Zune | 3 | ELECTRONICS | 7 | 6 | video-playback | true |
| 6 | 3 | Zune | 3 | ELECTRONICS | 10 | 6 | manufacturer | microsoft |
+----+----------+-------------------+------+-------------+------+----------+----------------+----------------+
13 rows in set (0.00 sec)
Ich hoffe, das ist das, was Sie suchen, seine stufenlos skalierbare Lösung, obwohl, wenn die Datenbank unglaublich mit einer hohen Arbeitsbelastung besteuert wird, könnte es verlangsamen , nur wegen seines Designs.
Gibt es eine eindeutige Liste der Kategorien, oder könnte jemand beschließt sie, eine Kategorie mit beliebigen Eigenschaften jederzeit hinzufügen? –