We have this one table that is really funky and I'm trying to get a descriptive result set out of it.
This table stores a tightly-related yet disparate collection of data types, and the meaningful column "names" are stored in a separate table. I think it'll be easier to show than to explain.
A Simplified and Abstract Example:
CREATE TABLE IF NOT EXISTS `something_obscure` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`obscure_type_id` tinyint(2) NOT NULL,
`segment1` varchar(92) DEFAULT NULL,
`segment2` varchar(92) DEFAULT NULL,
`segment3` varchar(92) DEFAULT NULL,
`datetime_created` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `something_obscure`
(`id`, `obscure_type_id`, `segment1`, `segment2`, `segment3`, `datetime_created`)
VALUES
('250', 1, '123', '456', '789', '2013-05-14 10:13:44'),
('251', 2, 'abc', 'def', 'ghk', NULL),
('252', 2, NULL, NULL, 'mnop', NULL);
CREATE TABLE IF NOT EXISTS `obscure_type` (
`id` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(30) NOT NULL,
`description` varchar(92) DEFAULT NULL,
`segment1` varchar(92) DEFAULT NULL COMMENT 'a LABEL/NAME for the data found in Segment1 of the Obscure table',
`segment2` varchar(92) DEFAULT NULL,
`segment3` varchar(92) DEFAULT NULL,
`datetime_created` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `obscure_type`
(`id`, `type`, `description`, `segment1`, `segment2`, `segment3`)
VALUES
(1, 'native', 'a descriptive description', 'best', 'worst', 'other'),
(2, 'alien', 'another descriptive desc', 'in', 'out', 'stationary');
Initially, I was expecting to have put meaningful names to the columns in the business logic (in php), but if I could just get SQL to handle that before our program even sees this result set, that'd be awesome!
GOAL:
Just to illustrate (I know this won't actually work), I was thinking something kinda like:
SELECT
data.segment1 AS type.segment1,
data.segment2 AS type.segment2,
data.segment3 AS type.segment3
FROM something_obscure AS data
JOIN pobscure_type AS type
ON data.obscure_type_id = type.id
WHERE data.obscure_type_id = 2
And that would return:
----------------------------
| in | out | stationary |
----------------------------
| abc | def | ghk |
| NULL | NULL | mnop |
----------------------------
SUMMARY:
How would one go about setting aliases to the values of another table?
We do not want to specify the alias name, we want the alias name set by the value of the referenced field. Is that possible?
Use single quotes around the aliases
Working fiddle demo
Other demo with type.*
Did you try to wrap aliases into backqoutes?