简体   繁体   English

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

[英]Query two databases in the same server

I have two databases in the same server and I need to: 我在同一服务器上有两个数据库,我需要:

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

But only the user_id list 但是只有user_id列表

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

Being the user_id field in the two tables the same. 两个表中的user_id字段相同。

I tried: 我试过了:

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

This is throwing the error: 这引发了错误:

.#1064 - You have an error in your SQL syntax; 。#1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '[database1].[dbo].[table1] as users INNER JOIN [database2].[dbo]' at line 2 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第2行的“ INNER JOIN [database2]。[dbo]”用户附近使用“ [database1]。[dbo]。[table1]”

NOTE: I'm using MySQL 注意:我正在使用MySQL

You can not have 2 where as you did ( since you tagged your question with Mysql) 您不能像以前那样在2个位置(因为您用Mysql标记了问题)

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

It should be 它应该是

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

Also while JOINING the 2 databases the syntax is 同样在加入2个数据库时,语法是

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

proj1 and project2 are 2 databases in the same server and users is the tables in those databases. proj1project2是同一服务器中的2个数据库,而users是这些数据库中的表。

For mysql, the square brackets [] are not used for object names. 对于mysql, square brackets []不用于对象名称。 Use back ticks instead 改用倒勾

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

There isn't a dbo schema in MySql as as per SqlServer. 根据SqlServer,MySql中没有dbo模式。 Just drop the dbo: 只需删除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

"where" is written two time.. “ where”写了两次。

try below: 请尝试以下方法:

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

OR Condition 或条件

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 

AND Condition 与条件

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 != '' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM