在MySQL中选择不具有公共字段名称的两个表中的公共id

时间:2017-03-10 11:07:25

标签: mysql

寻呼机表

+----+-------------+
| id | content     |
+----+-------------+
|  1 | bank        |
|  2 | invalid     |
|  3 | cat         |
+----+-------------+

页表

+-----------+--------------------+
| page_id   | content            |
+-----------+--------------------+
|  1        | people             |
|  3        | saved              |
+-----------+--------------------+

希望结果为

+-----------+--------------------+---------------+
| id        |        content     | page_content  |
+-----------+--------------------+---------------+
|  3        | cat                | null          |
+-----------+--------------------+---------------+

1 个答案:

答案 0 :(得分:0)

SELECT a.id, a.content, b.content AS page_content FROM page a JOIN pager b WHERE a.id = 3

输出将是:

+-----------+--------------------+---------------+
| id        |        content     | page_content  |
+-----------+--------------------+---------------+
|  3        | cat                | saved         |
+-----------+--------------------+---------------+