发布网友 发布时间:2022-04-26 13:40
共10个回答
懂视网 时间:2022-04-10 14:57
select id="findList" resultType="DoctorOrderDailyStatistic"> SELECT <include refid="doctorOrderDailyStatisticColumns" /> ,GROUP_CONCAT(c.BANK_CARD_NO SEPARATOR ",") AS "bankCardNo" FROM l_doctor_order_daily_statistic a left join l_account_bank_card c on a.doctorid=c.account_id <include refid="doctorOrderDailyStatisticJoins" /> <where> 1=1 and c.STATUS=1 <if test="doctorid != null and doctorid != ‘‘"> AND a.doctorid=#{doctorid} </if> <if test="statisticdate != null and statisticdate != ‘‘"> AND a.statisticdate=#{statisticdate} </if> <if test="beginDate != null and beginDate != ‘‘"> <![CDATA[ AND a.statisticDate >= #{beginDate} ]]> </if> <if test="endDate != null and endDate != ‘‘"> <![CDATA[ AND a.statisticDate <= #{endDate} ]]> </if> </where> GROUP BY a.doctorid <choose> <when test="page !=null and page.orderBy != null and page.orderBy != ‘‘"> ORDER BY ${page.orderBy} </when> <otherwise> </otherwise> </choose> </select>下面讲一下连表查询和合并字段
一、连表查询
连接查询包括合并、内连接、外连接和交叉连接,如果涉及多表查询,了解这些连接的特点很重要。 1、Union UNION 操作符用于合并两个或多个 SELECT 语句的结果集。 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。 当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。 两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 注意:使用UNION时,两张表查询的结果有相同数量的列、列类型相似。 学生表信息(Students):ID | Name | Age | City | MajorID |
101 |
Tom | 20 | BeiJing | 10 |
102 | Lucy | 18 | ShangHai | 11 |
ID | Name |
101 | Mrs Lee |
102 | Lucy |
SELECT ID,Name FROM Students UNION SELECT ID,Name FROM Teachers查询结果:
ID | Name |
101 | Mrs Lee |
101 | Tom |
102 | Lucy |
2)带条件的UNION查询,也可以查询同一张表,查询年龄为18,23岁的学生信息
SELECT ID,Name FROM Student WHERE Age=18 UNION SELECT ID,Name FROM Student WHERE Age=23
3)查询教师学生全部姓名 因为UNION只会选择不同的值,如果学生中和教师中有重名的情况,这就需要UNION ALL
SELECT Name FROM Students UNION ALL SELECT Name FROM Teachers查询结果:
ID | Name |
101 | Tom |
102 | Lucy |
101 | Mrs Lee |
102 | Lucy |
2、INNER JOIN(内连接) INNER JOIN(内连接),也成为自然连接 作用:根据两个或多个表中的列之间的关系,从这些表中查询数据。 注意: 内连接是从结果中删除其他被连接表中没有匹配行的所有行,所以内连接可能会丢失信息。 重点:内连接,只查匹配行。 语法:(INNER可省略)
SELECT fieldlist FROM table1 [INNER] join table2 ON table1.column=table2.column学生表信息(Students):
ID | Name | Age | City | MajorID |
101 | Tom | 20 | BeiJing | 10 |
102 | Lucy | 18 | ShangHai | 11 |
ID | Name |
10 | English |
12 | Computer |
实例:查询学生信息,包括ID,姓名、专业名称
SELECT Students.ID,Students.Name,Majors.Name AS MajorName FROM Students INNER JOIN Majors ON Students.MajorID = Majors.ID查询结果:
ID | Name | MajorName |
101 | Tom | English |
DELETE FROM Majors INSERT INTO Majors(ID,Name) VALUES(10,‘English‘) INSERT INTO Majors(ID,Name) VALUES(10,‘Computer‘)继续执行上面的关联语句,结果为:
ID | Name | MajorName |
101 | Tom | English |
101 | Tom | Computer |
ID | Name | MajorName |
101 | Tom | English |
101 | Tom | Computer |
102 | Lucy | NULL |
SELECT Students.ID,Students.Name,Majors.Name AS MajorName FROM Students LEFT JOIN Majors ON Students.MajorID = Majors.ID结果:
ID | Name | MajorName |
101 | Tom | English |
102 | Lucy | NULL |
SELECT Students.ID,Students.Name,Majors.Name AS MajorName FROM Students RIGHT JOIN Majors ON Students.MajorID = Majors.ID查询结果:
ID | Name | MajorName |
101 | Tom | English |
NULL | NULL | Computer |
SELECT Students.ID,Students.Name,Majors.Name AS MajorName FROM Students FULL JOIN Majors ON Students.MajorID = Majors.ID查询结果:
ID | Name | MajorName |
101 | Tom | English |
102 | Lucy | NULL |
NULL | NULL | Computer |
SELECT Students.ID,Students.Name,Majors.Name AS MajorName FROM Students CROSS JOIN Majors查询结果:
ID | Name | MajorName |
101 | Tom | English |
102 | Lucy | English |
101 | Tom | Computer |
102 | Lucy | Computer |
SELECT Students.ID,Students.Name,Majors.Name AS MajorName FROM Students,Majors3)加了查询条件 注意:在使用CROSS JOIN关键字交叉连接表时,因为生成的是两个表的笛卡尔积,因而不能使用ON关键字,只能在WHERE子句中定义搜索条件。
SELECT Students.ID,Students.Name,Majors.Name AS MajorName查询结果:
FROM Students CROSS JOIN Majors WHERE Students.MajorID = Majors.ID
ID | Name | MajorName |
101 | Tom | English |
SELECT am.activeId,m.modelName FROM activemodel am JOIN model m ON am.modelId = m.modelId ORDER BY am.activeId查询结果:
activeId | modelname |
1 | 吃饭 |
1 | 游泳 |
3 | 行车 |
4 | 唱歌 |
4 | 打球 |
修改过后的sql语句,查询后如图:
SELECT am.activeId,GROUP_CONCAT(m.modelName SEPARATOR ‘,‘) modelName FROM activemodel am JOIN model m ON am.modelId=m.modelId WHERE m.valid=1 GROUP BY am.activeId
查询结果:
activeId | modelName |
1 | 吃饭,游泳 |
3 | 行车 |
4 | 唱歌,打球 |
数据库联表查询及重复记录字段合并
标签:不同 一定要注意了 other 扩展 包括 eid let 显示 clu
热心网友 时间:2022-04-10 12:05
1、忽略表之间的关联关系
ALTER TABLE db2.dbo.table NOCHECK CONSTRAINT 关系名
2、--将没有重复的数据合并
insert into db2.dbo.table(field1,field2...) select field1,field2... from db1.dbo.table a where a.username not in (select username from db2.dbo.table)
3、将重复的数据写入临时表
select field1,field2... into 新的临时表 from db1.dbo.table a where a.username in (select username from db2.dbo.table)
热心网友 时间:2022-04-10 13:23
1.直接把结果更新在aaa表中的语句如下
update aaa
set post = (select sum_post from (select aaa.ID,(aaa.post+bbb.post) sum_post from aaa,bbb where aaa.ID=bbb.ID) t1 where t1.ID=a.ID)
where exists (select 1 from bbb where aaa.ID =bbb.ID);
2.直接查询显示的方法参见上楼;
3.新建ccc表:
create table ccc as( select id,username,sum(post) sum_post from
(select id,username,post from aaa
union all
select id,username,post from bbb)
group by id,username; )
热心网友 时间:2022-04-10 14:57
username 字段 是否是唯一字段 如果是唯一字段可以使用左连接的方式 UPDATE aaa 表 或BBB 表
update aaa LEFT JOIN bbb ON bbb.username =aaa.username set aaa.post=aaa.post+bbb.post.
或者 update bbb LEFT JOIN aaa ON aaa.username =bbb.username set bbb.post=aaa.post+bbb.post.
如果不是唯一字段的话 不能用username 作条件左连接了 如果ID是对应的用ID 左连接
update bbb LEFT JOIN aaa ON aaa.id =bbb.id set bbb.post=aaa.post+bbb.post.
热心网友 时间:2022-04-10 16:49
--建新表CCC
create table CCC
(ID int not null primary key
username varchar(20) not null
post int not null)
--将AAA中的数据复制到CCC里
declare @id int,@name varchar(20),@post int
declare yb cursor for
select id,username,post from AAA
yb open
fetch yb into @id,@name,@post
while @@fetch_status=0
begin
set identity_insert CCC on
inser into CCC(id,username,post) values(@id,@name,@post)
fetch yb into @id,@name,@post
end
close yb
deallocate yb
--CCC与BBB求和并更新到CCC表中
declare @sum int,@id1 int
declare yb1 cursor for
select B.id,B.post+C.post from BBB B join CCC C on B.id=C.id
yb1 open
fetch yb1 into @id1,@sum
while @@fetch_status=0
begin
set identity_insert CCC on
inser into CCC(post) values(@sum) where id=@id1
fetch yb into @sum
end
close yb1
deallocate yb1
热心网友 时间:2022-04-10 18:57
insert into table CCC
select aaa.username ,(aaa.post+bbb.post) as post
from _aaa表 , _bbb表
where aaa.username=bbb.username
热心网友 时间:2022-04-10 21:21
如果是线上数据,肯定不能手工合并,需要写个脚本,把两个表中的数据读出来,然后生成新字段对应的数据,再insert进新表中。如果数据量很大的情况下,建议采用增量更新,或者用队列。
热心网友 时间:2022-04-11 00:03
select id,username,sum(post) from
(select id,username,post from aaa
union all
select id,username,post from bbb)
group by id,username;
热心网友 时间:2022-04-11 03:01
select aaa.username, aaa.post+bbb.post into ccc where aaa.username=bbb.username
那个into 语句写前面还是最后我忘了,你可以试试看或者查查 select语句的手册
热心网友 时间:2022-04-11 06:15
看这里,看这里。
=====================================================
create table ccc select * from aaa limit 0;或者 create table ccc like aaa;
insert into ccc select aaa.id,aaa.username,aaa.post+bbb.post AS post from aaa,bbb where aaa.id=bbb.id;
select * from ccc;
=====================================================