【问题标题】:mysql how to merge /join/complete information from 1 rows to other row from the same tablemysql如何将1行的/join/complete信息合并到同一张表的另一行
【发布时间】:2012-12-08 12:18:14
【问题描述】:

我有一张这样的桌子

ID   | starttime | endtime  |manualid
1    | 12:24:00  | 13:25:00 |null
2    | null      | null     |1
3    | null      | null     |5
4    | 16:34:00  | 19:25:00 |null
5    | 21:40:00  | 23:25:00 |null

我想要的是当我让SELECT 有这样的东西时

ID   | starttime | endtime  |searchid
1    | 12:24:00  | 13:25:00 |null
2    | 12:24:00  | 13:25:00 |1
3    | 21:40:00  | 23:25:00 |5
4    | 16:34:00  | 19:25:00 |null
5    | 21:40:00  | 23:25:00 |null

这个想法是用他们的id从其他行的同一张表中的信息来完成一些行的信息

【问题讨论】:

    标签: mysql join merge


    【解决方案1】:

    我正在尝试在manualid = id 上加入 your_table。如果连接不成功(因为manualid is null,或者它的id不存在)t2.id将为空。

    如果连接不成功,我会从原始行中选择starttimeendtime,否则我会从另一行中选择:

    SELECT
      t1.ID,
      case when t2.id is null then t1.starttime else t2.starttime end as starttime,
      case when t2.id is null then t1.endtime else t2.endtime end as endtime,
      t1.manualid
    FROM
      your_table t1 left join your_table t2 on t1.manualid=t2.id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多