我的广告门户有 3 个表,我将 3 个表加入一个查询。
请查看此架构 http://sqlfiddle.com/#!2/8b74b/3
CREATE TABLE `ads` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`ads_title` CHAR(80) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `ads_keys` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`key` CHAR(25) NULL DEFAULT NULL ,
`inlist` INT(1) UNSIGNED ZEROFILL NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
);
CREATE TABLE `ads_values` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`ads_id` INT(3) NULL DEFAULT NULL,
`key_id` INT(10) NULL DEFAULT NULL,
`value` INT(10) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ads_id` (`ads_id`, `key_id`)
);
INSERT INTO `ads` VALUES (1, 'AAA');
INSERT INTO `ads` VALUES (2, 'BBB');
INSERT INTO `ads` VALUES (3, 'CCC');
INSERT INTO `ads` VALUES (4, 'DDD');
INSERT INTO `ads` VALUES (5, 'EEE');
INSERT INTO `ads` VALUES (6, 'FFF');
INSERT INTO `ads_keys` VALUES (1, 'KM', 1);
INSERT INTO `ads_keys` VALUES (2, 'OIL', 0);
INSERT INTO `ads_keys` VALUES (3, 'YEAR', 1);
INSERT INTO `ads_keys` VALUES (4, 'COLOR', 0);
INSERT INTO `ads_keys` VALUES (5, 'SPEED', 0);
INSERT INTO `ads_values` VALUES (1, 1, 1, 89000);
INSERT INTO `ads_values` VALUES (2, 1, 2, 200);
INSERT INTO `ads_values` VALUES (3, 1, 3, 2010);
INSERT INTO `ads_values` VALUES (4, 1, 4, 1);
INSERT INTO `ads_values` VALUES (5, 1, 5, 180);
INSERT INTO `ads_values` VALUES (6, 2, 1, 13000);
INSERT INTO `ads_values` VALUES (7, 2, 2, 150);
INSERT INTO `ads_values` VALUES (8, 2, 3, 2008);
INSERT INTO `ads_values` VALUES (9, 2, 4, 1);
INSERT INTO `ads_values` VALUES (10, 2, 5, 160);
INSERT INTO `ads_values` VALUES (11, 3, 1, 79800);
INSERT INTO `ads_values` VALUES (12, 3, 2, 172);
INSERT INTO `ads_values` VALUES (13, 3, 3, 2008);
INSERT INTO `ads_values` VALUES (14, 3, 4, 2);
INSERT INTO `ads_values` VALUES (15, 3, 5, 178);
INSERT INTO `ads_values` VALUES (16, 4, 1, 56781);
INSERT INTO `ads_values` VALUES (17, 4, 2, 127);
INSERT INTO `ads_values` VALUES (18, 4, 3, 2009);
INSERT INTO `ads_values` VALUES (19, 4, 4, 3);
INSERT INTO `ads_values` VALUES (20, 4, 5, 156);
INSERT INTO `ads_values` VALUES (21, 5, 1, 10200);
INSERT INTO `ads_values` VALUES (22, 5, 2, 205);
INSERT INTO `ads_values` VALUES (23, 5, 3, 2000);
INSERT INTO `ads_values` VALUES (24, 5, 4, 3);
INSERT INTO `ads_values` VALUES (25, 5, 5, 160);
INSERT INTO `ads_values` VALUES (26, 6, 1, 45877);
INSERT INTO `ads_values` VALUES (27, 6, 2, 150);
INSERT INTO `ads_values` VALUES (28, 6, 3, 2009);
INSERT INTO `ads_values` VALUES (29, 6, 4, 1);
INSERT INTO `ads_values` VALUES (30, 6, 5, 168);
我想低于查询结果
ads_id - ads_title - INLIST KEYS /*if into ads_keys table field's value 1 then this rows to columns*/
例如;
+----+-----------+-----------+------------+
| id | ads_title | KM | YEAR | -> If inlist=1 to columns to title
+----+-----------+-----------+------------+ (looks KM and YEAR inlist=1)
| 1 | AAA | val | val |
| 2 | BBB | val | val |
| 3 | CCC | val | val |
| 4 | DDD | val | val |
| 5 | EEE | val | val |
| 6 | FFF | val | val |
+----+-----------+-----------+------------+
我如何将这一行放到列中?以及我如何过滤此查询。例如颜色=1 或速度=160
编辑:行到列工作正常。谢谢@bluefeet。 我有新的要求。
请参阅http://sqlfiddle.com/#!2/8b74b/12。如何按显示的键过滤过滤器?