0

私は2つのテーブルを持っています:

CREATE TABLE `events` (
  `id` double NOT NULL,`value` int, PRIMARY KEY (`id`));

CREATE TABLE `updates` (
  `id` double NOT NULL AUTO_INCREMENT,
  `action` enum('delete','update') DEFAULT NULL,
  `event_id` double DEFAULT NULL,
  PRIMARY KEY (`id`));

eventsそして、イベントテーブルに更新が存在しない場合でも、更新からすべての値を取得し、更新から値を取得するクエリを作成しようとしていevent_id ます (アクションが「削除」の場合にのみ可能)

使用して

SELECT ifnull(e.id,u.event_id) AS event_id,
       e.value,ifnull(u.action,'update') AS ACTION,
       ifnull(u.id,-1) as update_id
        FROM events e 
         LEFT JOIN updates u ON e.id=u.event_id

私は得る

|      EVENT_ID | VALUE | ACTION | UPDATE_ID |
----------------------------------------------
| 1361264148710 |     1 | update |        -1 |
| 1361264148711 |     2 | update |        -1 |
| 1361264148712 |     5 | update |        -1 |
| 1361264148713 |    10 | update |         1 |
| 1361264148714 |    11 | update |         2 |
| 1361264148714 |    11 | update |         3 |
| 1361264148714 |    11 | update |         4 |
| 1361264148715 |    14 | update |         5 |
| 1361264148716 |     1 | update |         6 |
| 1361264148717 |    17 | update |         8 |
| 1361264148718 |    22 | update |        10 |
| 1361264148719 |    23 | update |        11 |

私の目標は取得することです:

|      EVENT_ID |  VALUE | ACTION | UPDATE_ID |
-----------------------------------------------
| 1361264148710 |      1 | update |        -1 |
| 1361264148711 |      2 | update |        -1 |
| 1361264148712 |      5 | update |        -1 |
| 1361264148713 |     10 | update |         1 |
| 1361264148714 |     11 | update |         2 |
| 1361264148714 |     11 | update |         3 |
| 1361264148714 |     11 | update |         4 |
| 1361264148715 |     14 | update |         5 |
| 1361264148716 |      1 | update |         6 |
| 1361264148708 | (null) | delete |         7 |
| 1361264148717 |     17 | update |         8 |
| 1361264148709 | (null) | delete |         9 |
| 1361264148718 |     22 | update |        10 |
| 1361264148719 |     23 | update |        11 |

ここにsqlfiddleがあります

4

1 に答える 1

0

Looks like you want to UNION your results:

SELECT ifnull(e.id,u.event_id) AS event_id,
       e.value,ifnull(u.action,'update') AS ACTION,
       ifnull(u.id,-1) as update_id
        FROM events e 
         LEFT JOIN updates u ON e.id=u.event_id
UNION
SELECT u.event_id AS event_id,
       e.value,ifnull(u.action,'update') AS ACTION,
       ifnull(u.id,-1) as update_id
        FROM updates u  
         LEFT JOIN events e ON e.id=u.event_id
     WHERE e.id IS NULL

And your updated fiddle: http://sqlfiddle.com/#!2/b1eb7/7

As @njk pointed out in the comments, this can be reduced to:

SELECT ifnull(e.id,u.event_id) AS event_id,
       e.value,ifnull(u.action,'update') AS ACTION,
       ifnull(u.id,-1) as update_id
FROM
  (SELECT id AS id FROM events
   UNION
   SELECT event_id AS id FROM updates) a
LEFT JOIN events e ON e.id = a.id
LEFT JOIN updates u ON u.event_id = a.id
ORDER BY update_id

Here is the updated Fiddle with both -- I think the first performs better looking at the execution plans.

于 2013-02-21T02:54:24.007 に答える