【问题标题】:SQL JOIN : Prefix fields with table nameSQL JOIN:表名前缀字段
【发布时间】:2015-10-27 09:40:03
【问题描述】:

我有以下表格

CREATE TABLE `constraints` (
  `id` int(11),
  `name` varchar(64),
  `type` varchar(64)
);

CREATE TABLE `groups` (
  `id` int(11),
  `name` varchar(64)
);

CREATE TABLE `constraints_to_group` (
  `groupid` int(11),
  `constraintid` int(11)
);

使用以下数据:

INSERT INTO `groups` (`id`, `name`) VALUES
(1, 'group1'),
(2, 'group2');

INSERT INTO `constraints` (`id`, `name`, `type`) VALUES
(1, 'cons1', 'eq'),
(2, 'cons2', 'inf');

INSERT INTO `constraints_to_group` (`groupid`, `constraintid`) VALUES
(1, 1),
(1, 2),
(2, 2);

我想获得所有组的所有约束,所以我执行以下操作:

SELECT groups.*, t.* FROM groups
LEFT JOIN
    (SELECT * FROM constraints
    LEFT JOIN constraints_to_group
    ON constraints.id=constraints_to_group.constraintid) as t
ON t.groupid=groups.id

并得到以下结果:

id|  name  | id |  name   type   groupid   constraintid   
-----------------------------------------------------
1 | group1 | 1  | cons1 |  eq  | 1       | 1
1 | group1 | 2  | cons2 | inf  | 1       | 2
2 | group2 | 2  | cons2 | inf  | 2       | 2

我想得到什么:

group_id | group_name | cons_id |  cons_name | cons_type | groupid | constraintid   
-------------------------------------------------------------------------------------
1        | group1     | 1       | cons1      |  eq       | 1       | 1
1        | group1     | 2       | cons2      | inf       | 1       | 2
2        | group2     | 2       | cons2      | inf       | 2       | 2

这是一个示例,在我的实际情况中,我的表有更多的列,因此使用 SELECT groups.name as group_name, ... 会导致查询非常难以维护。

【问题讨论】:

  • SELECT * 是 9/10 例是诅咒而不是祝福。手动指定您的列或使用动态 sql(这会很麻烦)
  • SELECT a.id as group_id ,a.name as group_name,..... FROM constraints_to_group as a JOIN groups on groups.id=a.groupid JOIN constraints as t on t.id=a.constraintid??
  • @عجمان 正如我在帖子的最后一句中所说,我正在寻找一种方法来为列名添加前缀,而无需逐个命名
  • 您可以使用动态 SQL(存储过程)来编写使用 Information_schema 数据库的查询。检索您需要的表的所有列,然后在列名之前使用表名编写查询。

标签: mysql sql


【解决方案1】:

试试这个方法

    SELECT groups.id as group_id,  groups.name as group_name ,
          t.id as cons_id,  t.name as cons_name,  t.type as cons_type,
          a.groupid ,  a.constraintid
   FROM constraints_to_group as a
    JOIN groups on groups.id=a.groupid
    JOIN constraints as t on t.id=a.constraintid

【讨论】:

  • 它产生了同样的问题:id |姓名 |编号 |姓名 |输入
  • 现在检查一下,我认为给定列的别名
【解决方案2】:

我看到的唯一区别是列的名称?用一个 AS 语句来表示。

SELECT 
groups.id AS group_id,
groups.name AS group_name,
t.id AS cons_id,
t.name AS cons_name,
t.groupid, t.constraintid
FROM groups
LEFT JOIN
(SELECT * FROM constraints
LEFT JOIN constraints_to_group
ON constraints.id=constraints_to_group.constraintid) as t
ON t.groupid=groups.id

此外,更好的连接构造是:

SELECT G.id AS group_id,
G.name AS group_name,
CG.id AS cons_id,
CG.name AS cons_name,
C.groupid, C.constraintid
FROM constraints_to_group CG
LEFT JOIN constraints C
ON CG.constraintid = C.id
LEFT JOIN groups G
ON CG.groupid = G.id;

【讨论】:

    【解决方案3】:

    issue 的可能副本

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 2016-07-03
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      相关资源
      最近更新 更多