2012-11-25 43 views
0

我有這兩個查詢無法執行工會,得到#1248 - 每一個派生表必須有它自己的別名錯誤

select 
    t . *, events.event_time as last_time 
from 
    events, 
(
    (
     select 
      bonding.type, 
       bonding.global_id2 as target_post, 
       bonding.target_id as on_whose_post, 
       GROUP_CONCAT(bonding.shooter_id) as shooter_ids, 
       GROUP_CONCAT(bonding.what_global_id) as shooted_what, 
       MAX(bonding.what_global_id) as last, 
       'bonding' as flag 
     from 
      bonding 
     where 
      bonding.type = 1 
       and bonding.shooter_id in (select 
        `user2` 
       from 
        relation_table 
       where 
        `user1` = 192) 
     group by bonding.global_id2 
    ) 
    union 
    (
    select 
      bonding.type, 
       bonding.global_id2 as target_post, 
       bonding.target_id as on_whose_post, 
       GROUP_CONCAT(bonding.shooter_id) as shooter_ids, 
       GROUP_CONCAT(bonding.what_global_id) as shooted_what, 
       MAX(bonding.what_global_id) as last, 
       'bonding' as flag 
     from 
      bonding 
     where 
      bonding.type = 2 
       and bonding.shooter_id in (select 
        `user2` 
       from 
        relation_table 
       where 
        `user1` = 192) 
     group by bonding.global_id2 
    ) 
    union 
    (
    select 
      bonding.type, 
       bonding.global_id2 as target_post, 
       bonding.target_id as on_whose_post, 
       GROUP_CONCAT(bonding.shooter_id) as shooter_ids, 
       GROUP_CONCAT(bonding.what_global_id) as shooted_what, 
       MAX(bonding.what_global_id) as last, 
       'bonding' as flag 
     from 
      bonding 
     where 
      bonding.type = 5 
       and bonding.shooter_id in (select 
        `user2` 
       from 
        relation_table 
       where 
        `user1` = 192) 
     group by bonding.global_id2 
    ) 
    union 
    (
    select 
      bonding.type, 
       bonding.global_id2 as target_post, 
       bonding.target_id as on_whose_post, 
       GROUP_CONCAT(bonding.shooter_id) as shooter_ids, 
       GROUP_CONCAT(bonding.what_global_id) as shooted_what, 
       MAX(bonding.what_global_id) as last, 
       'bonding' as flag 
     from 
      bonding 
     where 
      bonding.type = 9 
       and bonding.shooter_id in (select 
        `user2` 
       from 
        relation_table 
       where 
        `user1` = 192) 
     group by bonding.global_id2 
    ) 
    union 
    (
    select 
      bonding.type, 
       bonding.global_id2 as target_post, 
       bonding.target_id as on_whose_post, 
       GROUP_CONCAT(bonding.shooter_id) as shooter_ids, 
       GROUP_CONCAT(bonding.what_global_id) as shooted_what, 
       MAX(bonding.what_global_id) as last, 
       'bonding' as flag 
     from 
      bonding 
     where 
      bonding.type = 10 
       and bonding.shooter_id in (select 
        `user2` 
       from 
        relation_table 
       where 
        `user1` = 192) 
     group by bonding.global_id2 
    ) 

)as t where events.global_id = t1.last 

等一個: -

SELECT 
    post_stream.type, 
    post_stream.ref_global_id as target_post, 
    post_stream.user_id as on_whose_post, 
    post_stream.user_id as shooter_ids, 
    post_stream.ref_global_id as shooted_what, 
    post_stream.ref_global_id as last, 
    'stream' as flag, 
    events.event_time as last_time 
FROM 
    post_stream, 
    events, 
    relation_table 
WHERE 
    events.global_id = post_stream.ref_global_id 
     and post_stream.type IN (2 , 3, 7, 8) 
     AND post_stream.user_id = relation_table.user2 
     AND relation_table.user1 = 192 

現在我需要執行兩個查詢的聯合以獲得組合結果,但它給出了 每個派生表都必須有它自己的別名錯誤,我應該爲派生表放置一個別名,這兩個查詢在單獨運行時運行時沒有錯誤。

+0

它要求你在你的第一個查詢(每個'UNION(SELECT ...)'語句,包括第一個'SELECT'中別名子查詢,你也可以用完全相同的方式別名第二個查詢 – dash

+0

我是一些如何能夠解決問題,但這個查詢需要40秒完成...... – manish

+0

您需要停止使用'FROM a,b,c WHERE ... AND ...'表示法並使用顯式的'FROM a JOIN b ON ... JOIN c ON ...'表示法,你需要知道其他的存在,這樣你就可以讀取在90年代寫入的查詢,當JOIN的支持並不總是可用時,你絕不應該寫它自己 –

回答

1

第一個查詢的格式爲長約116行,包含一個5向UNION子查詢。這5個子查詢似乎與WHERE子句中的一個值相同。這個重寫將SQL簡化爲:

SELECT t.type, t.target_post, t.on_whose_post, t.shooter_ids, t.shooted_what, 
     t.last, t.flag, events.event_time AS last_time 
    FROM events JOIN 
     (SELECT bonding.type, 
       bonding.global_id2 AS target_post, 
       bonding.target_id AS on_whose_post, 
       GROUP_CONCAT(bonding.shooter_id) AS shooter_ids, 
       GROUP_CONCAT(bonding.what_global_id) AS shooted_what, 
       MAX(bonding.what_global_id) AS last, 
       'bonding' AS flag 
      FROM bonding 
     WHERE bonding.TYPE IN (1, 2, 5, 9, 10) 
      AND bonding.shooter_id IN (SELECT user2 FROM relation_table WHERE user1 = 192) 
     GROUP BY bonding.global_id2 
     ) AS t 
    ON events.global_id = t1.last 

這將更容易與第二個查詢結合起來。隨着進一步的修訂,我可能會刪除bonding.前綴,因爲主子查詢中唯一的表是bonding

SELECT p.type   AS type, 
     p.ref_global_id AS target_post, 
     p.user_id  AS on_whose_post, 
     p.user_id  AS shooter_ids, 
     p.ref_global_id AS shooted_what, 
     p.ref_global_id AS last, 
     'stream'  AS flag, 
     e.event_time AS last_time 
    FROM post_stream AS p 
    JOIN events   AS e ON e.global_id = p.ref_global_id 
    JOIN relation_table AS r ON p.user_id = r.user2 
WHERE r.user1 = 192 
    AND post_stream.type IN (2 , 3, 7, 8) 

問題:

第二個查詢應使用JOIN符號也被改寫

  1. 你肯定on_whose_postshooter_ids應該從同一列?
  2. 您確定shooted_whatlast應該來自同一列嗎?

這樣做可能是有效的(也不是太牽強)的原因 - 但這並不明顯。

不幸的是,我們還沒有被告知如何將第一個查詢中的數據與第二個查詢連接起來。似乎有很多相同的列,只有OP可以確定需要什麼。

+0

使用IN(1,2,5,9,10)不會給我所需的結果,因爲我想使用IN單獨分組具有{bonding.type = 1,bonding.type = 2,bonding.type = 5}的數據將把所有東西組合在一起。 – manish

+0

我是MySQL的新手,只是出於好奇心,我問的是使用「join a on b」語句比使用「from a,b」更好,或者它們都是相同的 – manish

+0

這是無稽之談。在type = 1的輸出中將會有一行,對於type = 2等的輸出將會有一行。您正在使用MySQL,它具有關於子查詢中的非聚合值如何以準隨機方式進行選擇的規則,但我沒有改變你的查詢部分。小心查詢中未列出GROUP BY子句中的非聚合列;它們是可移植性的責任,並且很可能是非確定性的。 –

相關問題