当前位置:首页>开发>正文

mysql怎么合并一个库中的所有表合并查询? MYSQL怎么把两张表合在一起来查找

2023-04-17 03:55:14 互联网 未知 开发

 mysql怎么合并一个库中的所有表合并查询? MYSQL怎么把两张表合在一起来查找

mysql怎么合并一个库中的所有表合并查询?

以MySQL数据库为例,通过SQL命令行将某个表的所有数据或指定字段的数据,导入到目标表中。此方法对于SQLServer数据库,也就是T-SQL来说,同样适用 。

类别一、 如果两张张表(导出表和目标表)的字段一致,并且希望插入全部数据,可以用这种方法:(此方法只适合导出两表在同一database)
INSERT INTO 目标表 SELECT * FROM 来源表
例如,要将 articles 表插入到 newArticles 表中,则可以通过如下SQL语句实现:
INSERT INTO newArticles SELECT * FROM articles

类别二、 如果只希望导入指定字段,可以用这种方法:
INSERT INTO 目标表 (字段1, 字段2, ...) SELECT 字段1, 字段2, ... FROM 来源表
请注意以上两表的字段必须一致(字段类型),否则会出现数据转换错误。

1、跨服务器复制表中数据
insert into openrowset(sqloledb,localhostsa123,Test.dbo.Table_B)
select * from Test.dbo.Table_A
//启用Ad Hoc Distributed Queries:
exec sp_configure show advanced options,reconfigure
exec sp_configure Ad Hoc Distributed Queries,reconfigure
//使用完成后,关闭Ad Hoc Distributed Queries:
exec sp_configure Ad Hoc Distributed Queries,0
reconfigure
exec sp_configure show advanced options,0
reconfigure

2、//不跨服务器
insert into dbo.Table_B) select * from dbo.Table_A
将表名和数据库连接字符串用代码拼接好 然后执行上述您需要的sql语句 程序功能即可完成

将一个mysql数据库中的一个表导入到另一个mysql数据库中

db1为原数据库,db2为要导出到的数据库,fromtable 是要导出的表名

1.方法一:
登录导出到的数据库,执行
create table fromtable select * from db1.fromtable
2.方法二:
在cmd下执行,mysqldump -u root -p db1 fromtable file=d:/fromtable.sql 输入秘密,root为用户名
登录db2 执行 source d:/fromtable.sql
3.方法三:
登录db1 执行 select * from fromtable into outfile "d:/fromtable .txt" 导出纯数据格式
登录db2 执行 load data infile d:/fromtable .txt into table fromtable 需要先建一张和原表结构一样的空表。
4.建一个odbc连接,先导出到access中,再导出到另一个库中。

MYSQL怎么把两张表合在一起来查找

俺不懂php,在此仅提供SQL。

select t.* from(
select a.列, a.date from a
union all
select b.列, b.date from b
) t
order by t.date
limit 0,
有问题,请追问。

mysql中怎么从一张表里将多张查询表的内容横向合并

selectfname 学生,
max(casewhenfcourse=语文thenfrecord else0 end) 语文,
max(casewhenfcourse=数学thenfrecord else0 end) 数学,
max(casewhenfcourse=英语thenfrecord else0 end) 英语
from表2 groupbyfname

mysql 多个表的信息怎么联合查询的

内联接:

select * from a
inner join b on a.field_name=b.field_name

左联接:
select * from a
left join b on a.field_name=b.field_name

右联接:
select * from a
right join b on a.field_name=b.field_name

建立外键(有2种方法0:

1、建表时指定
create table tb_name
(
id int,
num_id int references 要引用的表名(字段),
remark varchar(20)
)

2、建表后再添加外键
alter table table_name add constraint fk_name foreign key 要引用的表名(字段)

mysql每个月自动创建一张表,以年月做为表名,如何进行联合查询

我正好有楼主类似的需求,每个季度为几个表增加一个分区,表的基本名称是在一个叫设备类型的表里,每天计划执行一个过程,在过程里从系统表中判断是否已经创建了相关的分区,如果没创建就创建它

楼主可以参考一下,记得在my.ini 文件里配置event_scheduler=on
/**
定时每天检查各个设备类型的历史数据表,如果历史数据表的所在分区已经
接近当前日期,则为此设备类型追加分区
*/

-- 得到按月分区的日期值
delimiter 
drop function if exists fnGetPartitionDateForMonth 
delimiter 
create function fnGetPartitionDateForMonth() returns INT
begin
declare v_today datetime default date_add(now(), INTERVAL 2 month)
return year(v_today) * 100   month(v_today)
end

-- 得到按季度分区的日期值
delimiter 
drop function if exists fnGetPartitionDateForQuarter
delimiter 
create function fnGetPartitionDateForQuarter() returns int
begin
declare v_today datetime default date_add(now(), interval 3 month)
declare v_month int

set v_month = month(v_today)
if v_month = 1 or v_month = 2 or v_month = 3 then 
set v_today = DATE_ADD(v_today, INTERVAL (4 - v_month) month)
elseif v_month = 4 or v_month = 5 or v_month = 6 THEN
set v_today = DATE_ADD(v_today, INTERVAL (7 - v_month) month)
elseif v_month = 7 or v_month = 8 or v_month = 9 THEN
set v_today = date_add(v_today, INTERVAL (10 - v_month) month)
ELSE
set v_today = date_add(v_today, INTERVAL (13 - v_month) month)
end if

return year(v_today) * 100   month(v_today)
end

-- 得到按半年分区的日期值
delimiter 
drop function if exists fnGetPartitionDateForHalfYear
delimiter 
create function fnGetPartitionDateForHalfYear() returns int
begin
declare v_today datetime default date_add(now(), interval 6 month)
declare v_month int

set v_month = month(v_today)

if v_month <= 6 THEN
set v_today = date_add(v_today, INTERVAL (7 - v_month) month)
else
set v_today = DATE_ADD(v_today, INTERVAL (13 - v_month) month)
end if

return year(v_today) * 100   month(v_today)
end

-- 维护按年分区
delimiter 
drop function if exists fnGetPartitionDateForYear
delimiter 
create function fnGetPartitionDateForYear() returns int
begin
declare v_today datetime default date_add(now(), INTERVAL 2 year)
return year(v_today) * 100
end
delimiter 
drop procedure if exists spMaintainPartitions
delimiter 
create procedure spMaintainPartitions()
BEGIN

declare v_sql varchar(2000)
declare v_cnt int
declare v_deviceTypeId int
declare v_tablename varchar(50)
declare v_tablename_analog varchar(50)
declare v_tablename_digital varchar(50)
declare v_partitionType int
declare v_fileDir varchar(1000)
declare v_tablenames varchar(1000) default 
declare v_intDate int
declare v_partitionName varchar(100)
declare done int default 0
declare c_deviceType cursor 
for select Id, TableName, PartitionType, DataFileDir
from tbDeviceType 
where Generated = 1
declare continue handler for not found set done = 1

insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
Values(Now(), spMaintainPartitions start......)

open c_deviceType
deviceType_loop: LOOP

fetch c_deviceType into v_deviceTypeId, v_tablename, v_partitionType, v_fileDir

set v_fileDir = replace(v_fileDir, \, /)
if locate(:, v_fileDir) > 0 and locate(:/, v_fileDir) = 0 then
set v_fileDir = replace(v_fileDir, :, :/)
end if

if done = 1 then 
leave deviceType_loop
end if

set v_intDate = null
if v_partitionType = 1 then 
set v_intDate = fnGetPartitionDateForMonth()
ELSEIF v_partitionType = 2 THEN
set v_intDate = fnGetPartitionDateForQuarter()
ELSEIF v_partitionType = 3 then 
set v_intDate = fnGetPartitionDateForHalfYear()
elseif v_partitionType = 4 then 
set v_intDate = fnGetPartitionDateForYear()
end if

if v_intDate is null then
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`) 
values(Now(), Concat(DeviceTypeId = , cast(v_deviceTypeId As char(10)),  did not define paritition schedule))
else 

set v_partitionName = concat(p, cast(v_intDate as char(6)))

-- 模拟量表
set v_tablename_analog = concat(v_tablename, _Analog)
select count(*) into v_cnt
from information_schema.`TABLES` where `TABLE_SCHEMA` = database() and `table_name` = v_tablename_analog
if v_cnt > 0 then

select count(*) into v_cnt
from 
information_schema.`PARTITIONS` 
where 
TABLE_SCHEMA = database() and table_name = v_tablename_analog and partition_name = v_partitionName

if v_cnt = 0 then
set v_sql = CONCAT(alter table , v_tablename_analog,  add partition (partition , v_partitionName,  values less than (, cast(v_intDate as char(6)), ) data directory = , v_fileDir,  index directory = , v_fileDir , ))
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
Values(Now(), concat(sql = , v_sql))

set @sql = v_sql
prepare cmd from @sql
execute cmd
deallocate prepare cmd

insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`) 
values(Now(), concat(execute complete: , v_sql))
end if
end if

-- 数字量表
set v_tablename_digital = concat(v_tablename, _Digital)
select count(*) into v_cnt
from information_schema.`TABLES` where `TABLE_SCHEMA` = database() and `table_name` = v_tablename_digital

if v_cnt > 0 then

select count(*) into v_cnt
from 
information_schema.`PARTITIONS`
where 
TABLE_SCHEMA = database() and table_name = v_tablename_digital and partition_name = v_partitionName

if v_cnt = 0 then 
set v_sql = CONCAT(alter table , v_tablename_digital,  add partition (partition , v_partitionName,  values less than (, cast(v_intDate as char(6)), ) data directory = , v_fileDir,  index directory = , v_fileDir , ))

insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
Values(Now(), concat(sql = , v_sql))

set @sql = v_sql
prepare cmd from @sql
execute cmd
deallocate prepare cmd

insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`) 
values(Now(), concat(execute complete: , v_sql))

end if
end if

end if

end loop deviceType_loop
close c_deviceType

END

delimiter 

drop event if exists e_DataPartitionMaintain
create event e_DataPartitionMaintain
on SCHEDULE every 60 Second
on completion PRESERVE
do call spMaintainPartitions()

set global event_scheduler = on

MYSQL如何把两个结构相同的表组成一个表查询

select uid,sum(sc) sl from
(select uid,count(*) as sc from `bbs_topics` group by uid
union
select uid,count(*) as sc from `bbs_reply` group by uid)
group by uid

MySQL:如何将两张表的查询结果插入到一张新的表

表A ------------------- |id |user |info | |1 |u1 |991 | |3 |u3 |113 | ------------------- 表B ------------------- |id |user |pw |pw2 | |1 |u1 |p1 |p12 | |2 |u2 |p2 |p22 | |3 |u3 |p3 |p32 | ------------------- 能不能通过语句创建一个新表变成以下结果。剔除在表B里有。但是表A里没有的ID.并合并同ID的数据呢?当然是可以的。 ------------------------- |id |user |pw1 |pw2 |info | |1 |u1 |p1 |p12 |991 | |3 |u3 |p3 |p33 |113 | ------------------------- 两表的MYSQL查询结果插入新表的实现的语句

怎样把分散的数据库表整合到一个数据库表中?

首先确定下你的这些数据库表的表结构是否相同,如果相同,就可以用union函数来实现。
cerate table n
select * from a
union
select * from b
union
.....
以此类推。

最新文章