两张表在不同的数据库,如何关联查询?

两张表在不同的数据库,如何关联查询?,第1张

mysql支持多个库中不同表的关联查询,你可以随便链接一个数据库

然后,sql语句为:

select * from db1.table1 left join db2.table2 on db1.table1.id = db2.table2.id

只要用数据库名加上"."就能调用相应数据库的数据表了.

数据库名.表名

扩展资料

mysql查询语句

1、查询一张表:     select * from 表名;

2、查询指定字段:select 字段1,字段2,字段3....from 表名;

3、where条件查询:select 字段1,字段2,字段3 frome 表名 where 条件表达式;

例:select * from t_studect where id=1

  select * from t_student where age>22

4、带in关键字查询:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (21,23)     

   select * from t_student where age not in (21,23)

5、带between and的范围查询:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29

     select * frome t_student where age not between 21 and 29

可以参考下面的方法:

1、select * from 表1,表2,表3 where 表1.字段=表2.字段 and 表1.字段=表3.字段

2、select * from 表1 join 表2 on 表1.字段=表2.字段 and join 表3 on 表1.字段=表3.字段

如果没有AND,前面就需要加括号了。

扩展资料:

参考语句

创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根据已有的表创建新表: 

1、create table tab_new like tab_old (使用旧表创建新表)

2、create table tab_new as select col1,col2… from tab_old definition only

删除新表

drop table tabname 

参考资料来源:百度百科-SQL数据库

查询结果直接创建一个新表存放

select * into [新表名] FROM [原表名]WHERE 车辆='小汽车'

若新建表要放在另一个数据库B中

USE B

GO

SELECT * INTO [新表名] FROM [数据库名]..[表名]

WHERE 车辆='小汽车'

GO

SQL Server 是Microsoft 公司推出的关系型数据库管理系统。具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运行Microsoft Windows 2012 的大型多处理器的服务器等多种平台使用。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10715594.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-10
下一篇 2023-05-10

发表评论

登录后才能评论

评论列表(0条)

保存