查询同一服务器中的两个数据库

时间:2014-04-24 09:33:10

标签: mysql sql

我在同一台服务器上有两个数据库,我需要:

Select from table1 (in database1) the id field
Where notific1=1

但只有user_id列表

Where in table2 (in database2) the enabled field=1

两个表中的user_id字段相同。

我试过了:

SELECT table1.id 
    FROM [database1].[dbo].[table1] as users
    INNER JOIN [database2].[dbo].[table2] as subs
    ON users.user_id=subs.user_id
    WHERE users.notific1=1 AND
    WHERE subs.enabled=1

这引发了错误:

  

。#1064 - 您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便在#database1]附近使用正确的语法。[dbo]。[table1]作为用户INNER JOIN [database2]。[dbo]'在第2行

注意:我使用的是MySQL

5 个答案:

答案 0 :(得分:2)

你不能拥有2个(因为你用Mysql标记你的问题)

WHERE users.notific1=1 AND
WHERE subs.enabled=1

应该是

WHERE 
users.notific1=1 
AND
subs.enabled=1

同时,在加入2个数据库时,语法为

select * from proj1.users as y 
inner join project2.users f on f.email = y.email 
and y.email = 'abhik@xxxx.com'

proj1project2是同一服务器中的2个数据库,users是这些数据库中的表。

答案 1 :(得分:1)

对于mysql,square brackets []不用于对象名称。 请改用后退标记

SELECT table1.id 
    FROM `database1`.`table1` as users
    INNER JOIN `database2`.`table2` as subs
    ON users.user_id=subs.user_id
    WHERE users.notific1=1 AND
    subs.enabled=1

答案 2 :(得分:1)

MySql中没有dbo架构,就像每个SqlServer一样。只需删除dbo:

SELECT table1.id 
    FROM database1.table1 as users
    INNER JOIN database2.table2 as subs
    ON users.user_id=subs.user_id
    WHERE users.notific1=1 AND
    WHERE subs.enabled=1

答案 3 :(得分:1)

“where”写两次..

尝试以下:

SELECT table1.id 
    FROM database1.dbo.table1 as users
    INNER JOIN database2.dbo.table2 as subs
    ON users.user_id=subs.user_id
    WHERE users.notific1=1 AND
 subs.enabled=1

答案 4 :(得分:0)

OR条件

SELECT u1.*, 
       u2.* 
FROM   db1.table1 u1 
       INNER JOIN db2.table2 u2 
               ON u1.id = u2.id 
WHERE  u1.id IN( '12', '16' ) ** 
       OR ** u2.id = 17 

和条件

SELECT u1.*, 
       u2.* 
FROM   db1.table1 u1 
       INNER JOIN db2.table2 u2 
               ON u1.id = u2.id 
WHERE  u1.id IN( '12', '16' ) ** 
       AND ** u2.id != ''