【问题标题】:How to Clone all records in a Table while adding a string to name of record and incrementing the record ID如何克隆表中的所有记录,同时将字符串添加到记录名称并增加记录 ID
【发布时间】:2019-08-01 02:26:26
【问题描述】:

我通过将其他 2 个表与特定的 where 子句连接在一起创建了一个新表,这导致只有 169 条记录被添加到新表中。

我现在需要做的是以下

  1. 将所有这些记录克隆到当前表 (DUPLICATE)
  2. 将字符串添加到当前记录名称 例子 原始记录名称:记录 1 克隆记录名称:记录 1 - COPY

  3. 所有记录都有唯一的 ID 克隆时,ID 应该递增,但不能与其他记录的其他 ID 相同。

注意:这个新表中的记录不是下面的顺序示例

ID:bf378ee4-2430-264a-e7ec-546e68b12301

ID:bf378ee4-2430-264a-e7ec-546e68b12302

ID:bf378ee4-2430-264a-e7ec-546e68b12303

事实并非如此

Copy data to and from the same table and change the value of copied data in one column to a specified value

用于创建新表

create table sgr_New.tim_time_temp_merged_and_purged as
    SELECT * from sgr_New.tim_time as T
    join sgr_New.tim_time_cstm as C
    on T.id = C.id_c
    where
        (C.billable_time_c > "0")and
        (C.unbillable_time_c > "0")and
        (C.unbillable_reason_c not like "No_Unbillable_Time");

在网上找到这个(是否相关) 抱歉,我必须指定所有字段,因为我不应该插入一个特定字段。

insert into sgr_new.tim_time_temp_merged_and_purged (c1, c2, ...)
    select 
    unpaid_billable_revenue_c,
    unbilled_hours_c,
    unbillable_time_c,
    unbillable_reason_c,
    training_time_c,
    touched_c,
    total_time_c,
    time_netsuite_id_c,
    time_entered_c,
    tag_c,
    requirements_time_c,
    reporting_time_c,
    related_account_c,
    project_rate_c,
    project_or_case_name_c,
    project_number_c,
    project_mgmt_time_c,
    platform_build_time_c,
    overrun_c,
    num_minutes_c,
    num_hours_c,
    internal_notes_c,
    free_hours_c,
    expense_calculation_c,
    expense_amount_c,
    duration_c,
    dev_time_c,
    date_performed_c,
    data_load_time_c,
    currency_id,
    counter_field_c,
    configuration_time_c,
    category_c,
    case_rate_c,
    case_number_c,
    billing_rate_override_c,
    billing_rate_c,
    billing_notes_c,
    billed_time_c,
    billable_time_wf_copy_c,
    billable_time_override_c,
    billable_time_c,
    billable_override_c,
    billable_hours_kpi_c,
    billable_c,
    billable_amount_c,
    base_rate,
    amount_c,
    account_short_name_c
    from sgr_new.tim.time_cstm
    where id = 1;

我希望看到 我的表中的 338 条记录 ALL WITH UNIQUE ID's no duplicates 其中 169 个在记录名称中添加了“副本”

【问题讨论】:

  • INSERT into mytable (id, name, field1, field2, ...) SELECT max(id) + 1, concat('Copy of ', name), field1, field2, ... FROM mytable 认为我找到了解决方案
  • 您能否发布一些您的输入和预期输出数据。
  • 找到我的解决方案,感谢您的关注和帮助

标签: mysql sql mysql-workbench sugarcrm


【解决方案1】:

有一个 mysql 函数 UUID()(在 MSSQL 中:LOWER(NEWID()))可以不带参数调用以生成正确格式的 ID。

名称只需使用CONCAT(),但我看你自己已经找到了:)

【讨论】:

  • 是的,我找到了解决方案。感谢您的帮助。
【解决方案2】:

插入到 mytable ( ID, 姓名, 字段1, 字段2, ... ) SELECT max(id) + 1, concat('Copy of ', name), field1, field2, ... FROM mytable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多