发布网友 发布时间:2022-04-28 23:29
共5个回答
懂视网 时间: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
用一句SQL就好了,但是合并的前提条件要格式统一:热心网友 时间:2022-04-10 13:23
打开数据库B,新建一个查询,直接编写以下SQL语句(假设数据库A保存在E盘)热心网友 时间:2022-04-10 14:57
实现字段合并热心网友 时间:2022-04-10 16:49
新建一个查询,转换到SQL视图,假设表名分别为A和B,在查询里面输入: