Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (6.1k points)
edited by

I want to check (from the same table) if there is an association between the two events based on date-time.

One set of data has the ending date-time of certain events, and the other set of data has the starting date-time for other events.

If the first event gets completed before the second event then I want to link them up.

What I am having so far is:

SELECT name as name_A, date-time as end_DTS, id as id_A 

FROM tableA WHERE criteria = 1

SELECT name as name_B, date-time as start_DTS, id as id_B 

FROM tableA WHERE criteria = 2

Then I join them:

SELECT name_A, name_B, id_A, id_B, 

if(start_DTS > end_DTS,'VALID','') as validation_check

FROM tableA

LEFT JOIN tableB ON name_A = name_B

So based on my validation_check field, can I then run an UPDATE query with the SELECT nested?

1 Answer

0 votes
by (12.7k points)
edited by

You could actually do this one of two ways:

MySQL update join syntax:

UPDATE tableA a

INNER JOIN tableB b ON a.name_a = b.name_b

SET validation_check = if(start_dts > end_dts, 'VALID', '')

-- where clause can go here

ANSI SQL syntax:

UPDATE tableA SET validation_check = 

    (SELECT if(start_DTS > end_DTS, 'VALID', '') AS validation_check

        FROM tableA

        INNER JOIN tableB ON name_A = name_B

        WHERE id_A = tableA.id_A)

Choose whichever one appears most natural to you.

Want to be a SQL expert? Enroll at Intellipaat in this SQL Certification course.

If you want to know more about MySQL, refer to the below MySQL tutorial video:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.6k users

Browse Categories