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、创建两张测试表,
create table test_cj(name VARCHAR(20), remark varchar2(20))
create table test_kc(name VARCHAR(20), remark varchar2(20))
2、插入测试数据
insert into test_cj values('xh','cj_1')
insert into test_cj values('kcdh','cj_2')
insert into test_cj values('cj','cj_3')
insert into test_kc values('kcdh','kc_1')
insert into test_kc values('kcm','kc_2')
3、查询两张表的总记录数,select t.*, rowid from test_cj t union all select t.*, rowid from test_kc t,
4、编写sql,两张表进行关联,select t.name, t.remark, b.remark from test_cj t, test_kc b where t.name=b.name,可以发现关联出kcdh的记录,
数据库多表关联,一般采用外键比较方便,也可以额外建一个连接表做多表关联的连接,但这样稍微有点儿复杂,这些是建表方面的关联。查询关联,可以采用多表查询的方式关联查询,这点要求稍高点儿,但关联后再 *** 作单表时,别的表不用受太大的影响,这点特好。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)