Mysql从2个不同数据库的2个表中选择

时间:2015-04-11 20:49:39

标签: mysql

我遇到了这样的问题:

                SERVER
    database_1          database_2      
    table 'names'      table 'eans'
+----+--------------+   +----+--------------+
| ean | name        |   |ean | size         |
+----+--------------+   +----+--------------+
| 10 | name1        |   | 10 | 100          |
| 20 | name2        |   | 20 | 255          |
| 30 | name3        |   | 30 | 310          |    
| 40 | name4        |   | 40 | 350          |
| 50 | name5        |   | 50 | 10           |
+----+--------------+   +----+--------------+


I need for my output this

+----+--------+------+
| ean | name  | size |
+----+--------+------+
| 10 | name1  | 100  |
| 20 | name2  | 255  |
| 30 | name3  | 310  |    
| 40 | name4  | 350  |
| 50 | name5  | 10   |
+----+--------+------+

我发现需要使用UNION,但我不知道语法是否正确。
有人可以给我一些解决方法吗?

1 个答案:

答案 0 :(得分:1)

简单地在表之间连接,如database.tablename.fieldname ..,如下所示:

SELECT database_1.names.ean, name, size
FROM database_1.names
JOIN database_2.eans
     ON (database_1.names.ean = database_2.eans.ean)