I have a SQL statement that seems to not work with what I'm trying to do. I'm trying to select a dynamic column name by selecting a column value as a column name. However I get no errors and no values at all from the sub-select statement. Here is my query:
SELECT type,
hotel_name,
airport,
people,
rooms,
(IF(price_2017 IS NULL
OR price_2017 = '', IF(price_2016 IS NULL
OR price_2016 = '', price, price_2016), price_2017) + (
(
SELECT a.premium_col_name AS premium
FROM temp_db_calendar b
WHERE b.cdate='2016-02-13') * a.rooms)) AS the_price
FROM temp_db_cart a
WHERE a.airport = 'MCO'
AND a.people = '1'
AND a.type = 'Fly Snooze Cruise'
ORDER BY cast(the_price AS unsigned) ASC
Here is a sample of what temp_db_cart table looks like:
+-------------------+---------------------------------+---------+--------+-------+-----------+
| type | hotel_name | airport | people | rooms | the_price |
+-------------------+---------------------------------+---------+--------+-------+-----------+
| Fly Snooze Cruise | Holiday Inn Express | MCO | 1 | 1 | 99 |
| Fly Snooze Cruise | Econo Lodge Cocoa Beach | MCO | 1 | 1 | 109 |
| Fly Snooze Cruise | Econo Lodge Port Canaveral | MCO | 1 | 1 | 139 |
| Fly Snooze Cruise | Staybridge Suites Orlando | MCO | 1 | 1 | 141 |
| Fly Snooze Cruise | Fairfield Inn Orlando | MCO | 1 | 1 | 156 |
| Fly Snooze Cruise | SpringHill Suites Orlando | MCO | 1 | 1 | 159 |
| Fly Snooze Cruise | Hyatt Place Orlando | MCO | 1 | 1 | 170 |
| Fly Snooze Cruise | Hampton Inn Orlando | MCO | 1 | 1 | 179 |
| Fly Snooze Cruise | Hilton Cocoa Beach | MCO | 1 | 1 | 179 |
| Fly Snooze Cruise | International Palms Cocoa Beach | MCO | 1 | 1 | 183 |
| Fly Snooze Cruise | Wakulla Suites Cocoa Beach | MCO | 1 | 1 | 199 |
| Fly Snooze Cruise | The Inn at Cocoa Beach | MCO | 1 | 1 | 209 |
| Fly Snooze Cruise | DoubleTree Cocoa Beach | MCO | 1 | 1 | 217 |
| Fly Snooze Cruise | Hyatt Regency Orlando | MCO | 1 | 1 | 229 |
| Fly Snooze Cruise | Americas Best Cocoa | MCO | 1 | 1 | 300 |
+-------------------+---------------------------------+---------+--------+-------+-----------+
And here is the SQL for creating the temp_db_calendar table:
CREATE TABLE IF NOT EXISTS `temp_db_calendar` (
`cdate` date NOT NULL DEFAULT '2000-01-01',
`cday` int(2) unsigned NOT NULL DEFAULT '1',
`cmonth` int(2) unsigned NOT NULL DEFAULT '1',
`cyear` int(4) unsigned NOT NULL DEFAULT '2000',
`HolidayInnExpress_soldouts` int(255) NOT NULL,
`HolidayInnExpress_premiums` varchar(255) NOT NULL,
`AmericasBestCocoa_soldouts` int(11) NOT NULL,
`AmericasBestCocoa_premiums` varchar(255) NOT NULL,
`BestWesternTitusville_soldouts` int(11) NOT NULL,
`BestWesternTitusville_premiums` varchar(255) NOT NULL,
`BestWesternCocoa_soldouts` int(11) NOT NULL,
`BestWesternCocoa_premiums` varchar(255) NOT NULL,
`ComfortInnSanford_soldouts` int(11) NOT NULL,
`ComfortInnSanford_premiums` varchar(255) NOT NULL,
`DoubleTreeCocoaBeach_soldouts` int(11) NOT NULL,
`DoubleTreeCocoaBeach_premiums` varchar(255) NOT NULL,
`EconoLodgeCocoaBeach_soldouts` int(11) NOT NULL,
`EconoLodgeCocoaBeach_premiums` varchar(255) NOT NULL,
`EconoLodgePortCanaveral_soldouts` int(11) NOT NULL,
`EconoLodgePortCanaveral_premiums` varchar(255) NOT NULL,
`FairfieldInnOrlando_soldouts` int(11) NOT NULL,
`FairfieldInnOrlando_premiums` varchar(255) NOT NULL,
`HamptonInnOrlando_soldouts` int(11) NOT NULL,
`HamptonInnOrlando_premiums` varchar(255) NOT NULL,
`HiltonCocoaBeach_soldouts` int(11) NOT NULL,
`HiltonCocoaBeach_premiums` varchar(255) NOT NULL,
`HiltonRialtoMelbourne_soldouts` int(11) NOT NULL,
`HiltonRialtoMelbourne_premiums` varchar(255) NOT NULL,
`HolidayInnExpressCocoa_soldouts` int(11) NOT NULL,
`HolidayInnExpressCocoa_premiums` varchar(255) NOT NULL,
`HyattPlaceOrlando_soldouts` int(11) NOT NULL,
`HyattPlaceOrlando_premiums` varchar(255) NOT NULL,
`HyattRegencyOrlando_soldouts` int(11) NOT NULL,
`HyattRegencyOrlando_premiums` varchar(255) NOT NULL,
`InternationalPalmsCocoaBeach_soldouts` int(11) NOT NULL,
`InternationalPalmsCocoaBeach_premiums` varchar(255) NOT NULL,
`RamadaInnTitusville_soldouts` int(11) NOT NULL,
`RamadaInnTitusville_premiums` varchar(255) NOT NULL,
`SpringHillSuitesOrlando_soldouts` int(11) NOT NULL,
`SpringHillSuitesOrlando_premiums` varchar(255) NOT NULL,
`StaybridgeSuitesOrlando_soldouts` int(11) NOT NULL,
`StaybridgeSuitesOrlando_premiums` varchar(255) NOT NULL,
`TheInnatCocoaBeach_soldouts` int(11) NOT NULL,
`TheInnatCocoaBeach_premiums` varchar(255) NOT NULL,
`WakullaSuitesCocoaBeach_soldouts` int(11) NOT NULL,
`WakullaSuitesCocoaBeach_premiums` varchar(255) NOT NULL,
`test1` int(11) NOT NULL,
PRIMARY KEY (`cdate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Any help would be appreciated.
Update #1: I changed it to a subquery but still doesn't work:
SELECT type,
hotel_name,
airport,
people,
rooms,
(IF(price_2017 IS NULL
OR price_2017 = '', IF(price_2016 IS NULL
OR price_2016 = '', price, price_2016), price_2017) + (
(
SELECT (SELECT premium_col_name FROM temp_db_cart c WHERE a.url = c.url)
FROM temp_db_calendar b
WHERE b.cdate='2016-02-13') * a.rooms)) AS the_price
FROM temp_db_cart a
WHERE a.airport = 'MCO'
AND a.people = '1'
AND a.type = 'Fly Snooze Cruise'
ORDER BY cast(the_price AS unsigned) ASC