2

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
Daniel Harris
  • 1,805
  • 10
  • 45
  • 63
  • Hmmm . . . You are concatenating a *hotel_name* and then multiplying by *rooms*, and you are surprised that the results don't make sense? – Gordon Linoff Aug 28 '15 at 18:57
  • `select if(a.premium_col_name = 'colA', colA, if(a.premium_col_name = 'colB', colB, if(...)` – shibormot Aug 28 '15 at 19:56
  • It would be a lot easier to help you if you added a description of the two tables used together with some sample data. I'm guessing that `a.premium_col_name` column contains a column name in the `temp_db_calendar` from which you would like to retrieve a value? If this is the case you might need to use dynamic sql (maybe a prepared statement). – jpw Aug 28 '15 at 19:56
  • @jpw Yes that is correct. I'm unfamiliar with dynamic SQL though. – Daniel Harris Aug 28 '15 at 20:02
  • @Daniel Trying to inject the column name in the way you are trying will never work. If you have just a few fixed possible values in the premium_col_name to choose from then the method suggested by shibormot in a comment above should work, otherwise look at my answer. – jpw Aug 28 '15 at 20:51
  • @daniel I'm going to remove my answer as the assumptions I made doesn't seem to be correct. Looking at your data, what you should do if at all possible is to redesign your database by normalizing the temp_db_calendar table. It would save you a lot of trouble. – jpw Aug 28 '15 at 21:12
  • @jpw Unfortunately it's being run on a live shopping cart and someone else built it, so I am stuck with this table for now. – Daniel Harris Aug 28 '15 at 21:15

0 Answers0