【问题标题】:Combine 2 tables into 1将 2 个表合并为 1 个
【发布时间】:2016-05-29 02:24:47
【问题描述】:

如何将 2 个表合并为 1 个?也许使用 INNER JOIN?请指教。谢谢。

例如,使用我刚刚创建的以下数据库:

表 1

  +----------------------+
  | Places               |
  +----------------------+
  | Cupertino            |
  | BJ's Restaurant      |
  | Cupertino Inn        |
  | Outback Steakhouse   |
  +----------------------+ 

表 2

  +----------------------+
  | Distant              |
  +----------------------+
  | 0.6 km               |
  | 1.3 km               |
  | 1.3 km               |
  | 1.1 km               |
  +----------------------+ 

选择语法并组合此表输出将是这样的:

+---------------------+---------+
| Places              | Distant |
+---------------------+---------+
| Cupertino           | 0.6 km  |
| BJ's Restaurant     | 1.3 km  |
| Cupertino Inn       | 1.3 km  |
| Outback Steakhouse  | 1.1 km  |
+---------------------+---------+ 

**编辑:抱歉忘记添加 id。更新如下:

表 1

+-----------+--------------------+
|Places_Id  | Places             |
+-----------+--------------------+
| 1         | Cupertino          |
| 2         | BJs Restaurant     |
| 3         | Cupertino Inn      |
| 4         | Outback Steakhouse |
+-----------+--------------------+ 

表 2

+------------+----------+
|Distant_Id  | Distant  |
+------------+----------+
| 1          | 0.6 km   |
| 2          | 1.3 km   |
| 3          | 1.3 km   |
| 4          | 1.1 km   |
+------------+----------+ 

Places_Id 和 Distant_Id 是 Auto Increment。很抱歉,伙计们,我的大脑在很长一段时间没有进行相关编程后突然停止工作。事实证明,在将 id 添加到数据库后,这很容易。谢谢大家的答案。坚持下去。

Select A.Places, A.Places_Id, B.Distant, B.Distant_Id from Table1 A, Table2 B where B.Distant_Id = A.Places_Id

【问题讨论】:

  • 没有外键是不可能的
  • 这种设计很糟糕,您应该始终添加在两个关系表之间关联的外键。将列 ID 添加到表 1,将 place_id 添加到表 2
  • SQL 表代表 无序 集。无法(正确)组合这些表,因为值之间没有对应关系。
  • 您是否向我们展示了这两个表的所有列?您根据什么规则合并记录?为什么将“Cupertino”与“0.6 km”而不是“1.1 k`m”结合起来?
  • 因为这个值来自google距离矩阵api(距离)和places api(地点)。这意味着结果是从当前位置到目标位置的距离输出。当前位置是 Apple One 无限循环。这就是为什么您会在 Apple One 无限循环中看到持续 0.6 公里的库比蒂诺。

标签: mysql sql database sqlite


【解决方案1】:

这是一个使用用户变量的解决方案。

SQL:

-- Prepare data, just for demo
create table table1(Places varchar(200));
create table table2(Distant varchar(200));

insert into table1 values
('Cupertino'),
("BJ's Restaurant"),
('Cupertino Inn'),
('Outback Steakhouse');
insert into table2 values
('0.6 km'),
('1.3 km'),
('1.3 km'),
('1.1 km');
select * from table1;
select * from table2;

-- Process of combination
SET @rownum1:=0; 
SET @rownum2:=0; 
SELECT t1.Places, t2.Distant
FROM 
    (SELECT *, @rownum1:=@rownum1+1 AS rownum FROM table1) t1, 
    (SELECT *, @rownum2:=@rownum2+1 AS rownum FROM table2) t2
WHERE t1.rownum = t2.rownum ;

输出:

mysql> select * from table1;
+--------------------+
| Places             |
+--------------------+
| Cupertino          |
| BJ's Restaurant    |
| Cupertino Inn      |
| Outback Steakhouse |
+--------------------+
4 rows in set (0.00 sec)

mysql> select * from table2;
+---------+
| Distant |
+---------+
| 0.6 km  |
| 1.3 km  |
| 1.3 km  |
| 1.1 km  |
+---------+
4 rows in set (0.00 sec)

mysql>
mysql> -- Process of reporting
mysql> SET @rownum1:=0;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @rownum2:=0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT t1.Places, t2.Distant
    -> FROM
    ->     (SELECT *, @rownum1:=@rownum1+1 AS rownum FROM table1) t1,
    ->     (SELECT *, @rownum2:=@rownum2+1 AS rownum FROM table2) t2
    -> WHERE t1.rownum = t2.rownum ;
+--------------------+---------+
| Places             | Distant |
+--------------------+---------+
| Cupertino          | 0.6 km  |
| BJ's Restaurant    | 1.3 km  |
| Cupertino Inn      | 1.3 km  |
| Outback Steakhouse | 1.1 km  |
+--------------------+---------+
4 rows in set (0.00 sec)

【讨论】:

    【解决方案2】:

    问题在于您无法“对应”两个表中的值,因为 SQL 表代表 无序 集。如果您要从外部源将数据加载到表中,请包含 auto_increment 列以获取原始排序。

    缺少这些,您可以通过枚举每个表中的行并加入/聚合来获得您想要的东西。这接近你想要的:

    select max(col1) as col1, max(col2) as col2
    from ((select t1.col as col1, NULL as col2, (@rn1 := @rn1 + 1) as seqnum
           from table1 t1 cross join (select @rn1 := 0) params
          ) union all
          (select NULL, t2.col, (@rn2 := @rn2 + 1) as seqnum
           from table2 t2 cross join (select @rn2 := 0) params
          )
         ) tt
    group by seqnum;
    

    这并不能保证表格中的原始顺序,但它会生成一个新表格,将其他两个表格中的每一行都精确地组合在一起。

    【讨论】:

      【解决方案3】:

      如果你添加一些 id,那么你可以加入:

      SELECT A.field, A.otherfield, B.field, B.otherfield, C.yetanotherfield
      FROM table_A AS A
      JOIN table_B as B on A.id = B.a_id
      JOIN table_C as C on B.id = C.b_id
      

      【讨论】:

        【解决方案4】:

        table2中需要有一列来标识table1的哪个条目属于table2中的条目,所以需要一个外键。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-05
          • 2021-11-19
          • 1970-01-01
          • 2020-12-06
          • 1970-01-01
          相关资源
          最近更新 更多