【问题标题】:Add ID and Type from supplemental tables to parent table using SQL (MySQL & Laravel Polymorphic relations)使用 SQL(MySQL 和 Laravel 多态关系)将补充表中的 ID 和类型添加到父表
【发布时间】:2018-08-09 22:42:58
【问题描述】:

注意。 Also posted on Database Administrators

我有一个记录所有销售的表格和十四个包含有关销售的额外信息的补充表格。十四个补充表格的所有意图和目的都是相同的。它们是很久以前创建的,当时最初的开发人员认为会有更多的差异,但实际上现在项目已经成熟,它们的相似之处多于不同之处。然而,它们不同的,因此我需要将它们分开。

当前结构

销售表

| id      | customer_id | customer_ref                         | ... |
|---------|-------------|--------------------------------------|-----|
| 1237567 | 354         | a6143f8c-b679-47be-9bc0-52457842913c | ... |
| 1237568 | 867         | ref89b72                             | ... |
| ...     | ...         | ...                                  | ... |

补充表1类:App\SuppOne

| id   | customer_id | customer_ref                         | ... |
|------|-------------|--------------------------------------|-----|
| 2857 | 10372       | 2016-07-01-ab5d09cc37ca              | ... |
| 2858 | 354         | a6143f8c-b679-47be-9bc0-52457842913c | ... |
| ...  | ...         | ...                                  | ... |

补充表2类:App\SuppTwo

| id    | customer_id | customer_ref | ... |
|-------|-------------|--------------|-----|
| 90488 | 867         | ref89b72     | ... |
| 90489 | 1024        | 0000080992   | ... |
| ...   | ...         | ...          | ... |

表上没有将销售表连接到补充表的外键,但是有一个“customer_id”和“customer_reference”,它们对于销售表和补充表都是唯一的,但它们并不一致。这是目前用于在我需要获取有关给定销售的更多信息时加入两者的方法。

我正在使用 Laravel 5.1 和 MySQL 数据库,我想在 sales 表中添加两个字段; supplemental_id 和supplemental_type 以便快速有效地创建多态关系。

所需结构

销售表

| id      | supplemental_id | supplemental_type | customer_id | customer_ref                         | ... |
|---------|-----------------|-------------------|-------------|--------------------------------------|-----|
| 1237567 | 2858            | App\SuppOne       | 354         | a6143f8c-b679-47be-9bc0-52457842913c | ... |
| 1237568 | 90488           | App\SuppTwo       | 867         | ref89b72                             | ... |
| ...     | ...             | ...               | ...         | ...                                  | ... |

我需要将这两个字段添加到每个销售记录中,但我不确定如何使用原始 SQL 来执行此操作,因为我预计它会比在迁移中完成要快得多。我想知道如何(如果可能)在 SQL 中处理从 table_name 到 App\ClassName 的映射。 sales 表中有大约 150 万条记录,循环遍历它们不会花费太多时间。

【问题讨论】:

  • 它基本上是一个带有双连接选择的更新。虽然老实说,当 Select 会处理视觉效果时,你为什么要这样做?
  • 我想知道如何(如果可能的话)使用 SQL 完成它,因为在迁移中使用 PHP 完成它需要太长时间。
  • INSERT INTO Table_New SELECT * FROM Table1,Table2 WHere Table1.key1 = table2.key1 ?
  • table_name 到 App\ClassName 的映射如何处理?

标签: mysql laravel-5 laravel-5.1 polymorphic-associations


【解决方案1】:

字里行间的东西。

对于特定的销售记录,它可能会覆盖数据(即 SuppFourteen 会覆盖 SuppOne 数据),但这就是您在问题中呈现它的方式。

$fourteen_tables = [
    'supplemental_table_1' => App\SuppOne::class,
    // ...
];

foreach ($fourteen_tables as $table => $class) {
    DB::table('sales_table')
        ->join($table, function ($join) use ($table) {
            $join->on($table.'.customer_id', '=', 'sales_table.customer_id')
                ->on($table.'.customer_ref', '=', 'sales_table.customer_ref');
        })
        ->update([
            'sales_table.supplemental_id' => DB::raw($table.'.id'),
            'sales_table.supplemental_type' => $class,
        ]);
}

【讨论】:

  • 感谢 Jareck,关于连接表(在极少数情况下可能有多个匹配的 customer_id 和 customer_reference)可以排序以获取最后一条记录吗?
  • 在更新调用中,我尝试了DB::raw("(select max({$table}.id))"),但这似乎没有按预期工作。在类似的注释中,我想在连接中添加一个 WHERE 子句,以便仅匹配 status 为“1”的记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
相关资源
最近更新 更多