程序go 2009-08-11 20:39 采纳率: 100%
浏览 210
已采纳

基于 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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题