0

私はこれらの2つのクエリを持っています

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

および他の1つ:-

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

結合された結果を取得するには、両方のクエリで結合を実行する必要がありますが、すべての派生テーブルには独自のエイリアスエラーが必要です。派生テーブルのエイリアスを配置する必要があります。これら2つのクエリは、別々に実行するとエラーなしで実行されます。 。

4

1 に答える 1

1

最初のクエリはフォーマットされた長さが約116行で、5方向のUNIONサブクエリが含まれています。これらの5つのサブクエリは、WHERE句の1つの値を除いて同一であるように見えます。この書き直しにより、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

これは、2番目のクエリと組み合わせるのがはるかに簡単になります。bonding.さらに改訂すると、メインサブクエリのテーブルは。だけなので、プレフィックスを削除する可能性がありますbonding

2番目のクエリもJOIN表記を使用して書き直す必要があります。

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)

質問:

  1. 確かに、同じ列からのものである必要がありますかon_whose_postshooter_ids
  2. 確かに、同じ列からのものである必要がありますかshooted_whatlast

それを行うのには正当な(そしてあまりにも大げさではない)理由があるかもしれませんが、それはすぐには明らかではありません。

残念ながら、上記の最初のクエリのデータを2番目のクエリと結合する方法は説明されていません。共通する列はかなり多いようです。OPだけが必要なものを決定できます。

于 2012-11-25T13:12:49.427 に答える