Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9318473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:08:13+00:00 2026-06-19T03:08:13+00:00

I have two tables : CREATE TABLE `events` ( `id` double NOT NULL,`value` int,

  • 0

I have two tables :

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`));

And trying to make query which will get all values from events and get values from updates, even if event_id in updates do not exist in events table (only possible if action is ‘delete’)

using

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

I getting

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

my goal is to get:

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

here is sqlfiddle

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:08:14+00:00Added an answer on June 19, 2026 at 3:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two SQL statements: CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
I have two tables: CREATE TABLE `category` ( `category_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I have the following tables: CREATE TABLE `attendance_event_attendance` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have two tables: CREATE TABLE Event_details( event_no INTEGER AUTOINCREMENT NOT NULL, no_players INTEGER
I have two tables: codes_tags and popular_tags. codes_tags CREATE TABLE `codes_tags` ( `code_id` int(11)
I have an events table with two columns eventkey (unique, primary-key) and createtime ,
Lets say I have these two tables: CREATE TABLE nodes ( id INTEGER PRIMARY
I have two tables, round and event. One round has many events. create table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.