I have 3 tables - things, defs, and info (terrible names, but significantly reduced for simplicity!)
info
CREATE TABLE `info` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`thingid` bigint(10) NOT NULL DEFAULT '0',
`defid` bigint(10) NOT NULL DEFAULT '0',
`data` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY `infodata_coufie_ix` (`thingid`,`defid`)
);
id | thingid | defid | data
1 | 1 | 1 | 1
1 | 1 | 2 | 25
1 | 2 | 1 | 0
1 | 2 | 3 | yellow
1 | 3 | 1 | 0
defs
CREATE TABLE `defs` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`datatype` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
id | name | datatype
1 | enabled | boolean
2 | size | numeric
3 | colour | string
things
CREATE TABLE `things` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
id | name
1 | bill
2 | terry
3 | nancy
I'd like to be able to show the "defs"' value of "things", so a resultant table / view would be something like
thingid | name | enabled | size | colour
1 | bill | true | 25 | null
2 | terry | false | null | yellow
3 | nancy | true | null | null
so the rows from defs
would become column headers; the values matching those column headers for thingid
would form the data for those rows.
I've done this long ago in SQL Server, and for the life of me can't remember how to do it. I now need to do it in MySql5. I've been reading up and down http://www.artfulsoftware.com/infotree/queries.php and various SE articles but I've now confused the heck out of myself, so I have to actually ask someone.