I've searched and viewed various posts about pivoting (dynamically) data from MySQL. I've managed to get it working with one table, but I am hitting a block when I attempt to do this with three tables. Can someone point me in the right direction?
- Table 1: Vendors (List of Vendors we buy from)
- Table 2: Criteria (The criteria each Vendor sets)
- Table 3: Matrix (A link table for the above two)
What I would like to achieve is a matrix/pivot like this:
Criteria1 Criteria2 Criteria3
Vendor 1 ValueFromMatrix ValueFromMatrix ValueFromMatrix
Vendor 2 ValueFromMatrix ValueFromMatrix ValueFromMatrix
Vendor 3 ValueFromMatrix ValueFromMatrix ValueFromMatrix
An example of this table in use:
MinOrderQty MinOrderValue ReturnsAllowed
Intel 1000 5000.00 RMA required
AMD 2000 15000.00 No
nVidia 9000 8000.00 RMA + dropship
The number of Vendors and Criteria is dynamic and can be added/deleted by users, so I can't hard code the transform from rows to columns.
The ValueFromMatrix is a Varchar(200) field and contains text. It's linked two the other two tables by the use of a VendorID and a CriteriaID (int) fields.
I simply have hit a brick wall here, and I'm not having any luck deciphering some of the examples I've seen.
Table code:
CREATE TABLE `vendors` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`vendor_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `criteria` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`criteria_desc` varchar(150) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `matrix` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vendor_id` int(11) DEFAULT NULL,
`criteria_id` int(11) DEFAULT NULL,
`criteria_response` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=latin1;
The criteria_response field is what populates the ValueFromMatrix in the table example above.
If anyone is able to point me in the right direction, I'd be grateful. I've just not seen any examples that discuss three tables to generate this view, and I keep getting stuck.
Thanks.