从mysql中的两个不同表生成别名列

am46iovg  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(304)

我有一个与两个不同表相关的数据库表,例如:

=== inventory ===
+------------+-----------+
|    code    |   total   |
+------------+-----------+
| el_pr_25   |     45    |
| el_pr_11   |     33    |
| mob_tp_x93 |     23    |
| mob_tp_t55 |     33    |
| el_pr_x73  |     25    |
| mob_tp_25  |     22    |
+------------+-----------+

= electricity =
+-----+-------+
|  id | name  |
+-----+-------+
|  25 | test1 |
|  11 | test2 |
| x73 | test3 |
+-----+-------+

  == mobile ==
+-----+----------+
|  id |   name   |
+-----+----------+
| x93 | test 66  |
| t55 | test 222 |
| 25  | test 323 |
+-----+----------+

我要选择的主表是inventory表,inventory表通过 code 列,对于电能表有一个 el_pr_ 前缀后跟 id 电力表和移动表的前缀是 mob_tp_ 是前缀,我想从电力和移动表中选择名称列为的库存表,例如结果如下:

SELECT code,total, ... as name FROM inventory; 

         === inventory ===
+------------+-----------+----------+
|    code    |   total   |  name    |
+------------+-----------+----------+
| el_pr_25   |     45    | test1    |
| el_pr_11   |     33    | test2    |
| mob_tp_x93 |     23    | test 66  |
| mob_tp_t55 |     33    | test 22  |
| el_pr_x73  |     25    | test3    |
| mob_tp_25  |     22    | test 323 |
+------------+-----------+----------+
rjzwgtxy

rjzwgtxy1#

你可以用 inner joinunion [all] 作为:

select i.*, e.name from inventory i 
                   inner join electricity e on ( i.code = concat('el_pr_',e.id) ) union all
select i.*, m.name from inventory i 
                   inner join mobile m on ( i.code = concat('mob_tp_',m.id) );

rextester演示

brgchamk

brgchamk2#

我们可以尝试以下联接查询:

SELECT
    i.code,
    i.total,
    COALESCE(e.name, m.name) AS name
FROM inventory i
LEFT JOIN electricity e
    ON i.code REGEXP CONCAT('el_.*_', e.id, '$')
LEFT JOIN mobile m
    ON i.code REGEXP CONCAT('mob_.*', m.id, '$');

演示

上面的查询使用 COALESCE 为每个项目选择正确名称的技巧,它假定给定的项目只与 electricity 或者 mobile table。
但是,您的数据库设计并不理想。最好只有一个表包含移动和电气(以及其他)项目的元数据。此外,您的表应该有适当的联接列,这些列不需要复杂的子字符串或regex操作来匹配。我建议如下:

inventory
+----+------------+-----------+
| id |    code    |   total   |
+----+------------+-----------+
| 1  | el_pr_25   |     45    |
| 2  | el_pr_11   |     33    |
| 3  | el_pr_x73  |     25    |
| 4  | mob_tp_x93 |     23    |
| 5  | mob_tp_t55 |     33    |
| 6  | mob_tp_25  |     22    |
+----+------------+-----------+

items
+--------------+----------+-------------+
| inventory_id | name     | type        |
+--------------+----------+-------------+
| 1            | test1    | electricity |
| 2            | test2    | electricity |
| 3            | test3    | electricity |
| 4            | test 66  | mobile      |
| 5            | test 222 | mobile      |
| 6            | test 323 | mobile      |
+--------------+----------+-------------+
mqkwyuun

mqkwyuun3#

我建议将您的数据模型更改为单独的列,并使用两个列形成的唯一约束。

|code  |   total   |type
+------+-----------+
| 25   |     45    |el_pr_
| 11   |     33    |el_pr_
| x93  |     23    |mob_tp_
| t55  |     33    |mob_tp_
| x73  |     25    |el_pr_
| 25   |     22    |mob_tp_

并将名称的来源合并到一个表中

|  id | name  |type
+-----+-------+
|  25 | test1 |el_pr_
|  11 | test2 |el_pr_
| x73 | test3 |el_pr_
| x93 | test 66  |mob_tp_
| t55 | test 222 |mob_tp_
| 25  | test 323 |mob_tp_

然后

select *
from inventory i
join tab_names n on i.type = n.type and i.code = n.id

这只是一个粗略的轮廓!

相关问题