MySQL - 从列值中选择列名

时间:2015-08-28 18:53:41

标签: mysql sql select subquery

我有一个SQL语句似乎与我尝试做的事情无关。我通过选择列值作为列名来尝试选择动态列名。但是,从sub-select语句中我没有任何错误,也没有任何值。这是我的疑问:

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

以下是temp_db_cart表的示例:

+-------------------+---------------------------------+---------+--------+-------+-----------+
|       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 |
+-------------------+---------------------------------+---------+--------+-------+-----------+

这是用于创建temp_db_calendar表的SQL:

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;

任何帮助都将不胜感激。

更新#1:我将其更改为子查询但仍然无效:

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

0 个答案:

没有答案