derek5. 2009-08-11 20:39 采纳率: 100%
浏览 407
已采纳

基于 SELECT 查询的 MySQL-UPDATE 查询

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

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

If the first event completes before the second event then I would like to link them up.

What I have 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

Can I then, based on my validation_check field, run a UPDATE query with the SELECT nested?

转载于:https://stackoverflow.com/questions/1262786/mysql-update-query-based-on-select-query

  • 写回答

12条回答 默认 最新

  • 乱世@小熊 2009-08-11 20:53
    关注

    You can 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)
    

    Pick whichever one seems most natural to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化