将2个表合并为1个

时间:2016-02-17 11:43:10

标签: mysql sql database sqlite

如何将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

4 个答案:

答案 0 :(得分:4)

问题是你无法“对应”两个表中的值,因为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;

这并不能保证表中的原始排序,但它会生成一个新表,其中只有一行与其他两个表中的每一行完全相同。

答案 1 :(得分:1)

在table2中,你需要有一个列来标识table1的哪个条目属于table2中的条目,因此需要一个外键。

答案 2 :(得分:1)

如果您添加了一些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

答案 3 :(得分: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)