2017-01-01 76 views
0

我查詢MySQL數據庫在2008年SSRS的報告,我用下面連接兩個不同的查詢也沒問題......結合3次不同的查詢

SELECT a.arrivalDate, a.order_item_id, b.roomType 
FROM 
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a, 
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b 
WHERE a.order_item_id = b.order_item_id 

...但是當我嘗試添加第三個,它的服用問題,我的WHERE子句,但我本可以發誓我用來做什麼的這樣:

SELECT a.arrivalDate, a.order_item_id, b.roomType, c.retreatDate 
FROM 
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a, 
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b, 
(select order_item_id, meta_value as retreatDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'retreat-date') as c, 
WHERE a.order_item_id = b.order_item_id AND a.order_item_id = c.order_item_id 

有什麼建議?

+0

順便說一句,這不是我怎麼會做 - 但與SSRS標籤是什麼? – Strawberry

+0

它適用於SSRS,並且它在SQL Server Report Builder – Tim

+0

如果您知道另一種效率更高的方法,歡迎提供建議。 – Tim

回答

0

沒關係,明白了!我以爲這是我最初嘗試過的,但我一定有一個錯字。希望我的錯誤將會幫助其他人在未來過這個絆腳石:

SELECT a.arrivalDate, a.order_item_id, b.roomType, c.retreatDate 
FROM 
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a, 
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b, 
(select order_item_id, meta_value as retreatDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'retreat-date') as c 
where a.order_item_id = b.order_item_id AND b.order_item_id = c.order_item_id 
0

我要堅持無論是傳統的解決方案 - 首先是(IMO)更易於閱讀。二是通常速度更快...

查詢1

SELECT order_item_id 
    , MAX(CASE WHEN meta_key = 'Arrival Date' THEN meta_value END) arrivalDate 
    , MAX(CASE WHEN meta_key = 'type-of-room' THEN meta_value END) roomType 
    , MAX(CASE WHEN meta_key = 'retreat-date' THEN meta_value END) retreatDate 
    FROM wp_woocommerce_order_itemmeta 
GROUP 
    BY order_item_id 

查詢2

SELECT DISTINCT x.order_item_id 
       , arrival.meta_value arrivalDate 
       , room.meta_value roomType 
       , date.meta_value retreatDate 
      FROM wp_woocommerce_order_itemmeta x 
      LEFT 
      JOIN wp_woocommerce_order_itemmeta arrival 
      ON arrival.order_item_id = x.order_item_id 
      AND arrival.meta_key = 'Arrival Date' 
      LEFT 
      JOIN wp_woocommerce_order_itemmeta room 
      ON room.order_item_id = x.order_item_id 
      AND room.meta_key = 'type-of-room' 
      LEFT 
      JOIN wp_woocommerce_order_itemmeta date 
      ON date.order_item_id = x.order_item_id; 
+0

好的,謝謝你的建議。我感謝你的時間。 – Tim