您好,欢迎访问一九零五行业门户网

mysql中关联变量条件修改、查询以及数据显示成一行的介绍

本篇文章给大家带来的内容是关于mysql中关联变量条件修改、查询以及数据显示成一行的介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
一对多数据显示成一行
group_concat(expr)
1、涉及的表关系:teacher表、teacher_subject_rel表(教师所能教的学科表)、subject表
2、业务场景: 需要拉取所有教师的编号(teacher_no)、学科名(subject_name)。   教师表(teacher)和学科(teacher_subject_rel)是一对多关系,  往往查询出现的是同一教师多条    数据。我们希望得到每个教师一条数据 学科拼接成一条
1、基本语法
group_concat( [distinct]  要连接的字段   [order by 排序字段 asc/desc]   [separator '分隔符'] )
2、例子
select    t.teacher_id as '教师id',    t.teacher_no '教师编号', (    select        group_concat(s.subject_name)    from        teacher_subject_rel tsr    left join `subject` s on tsr.subject_id = s.subject_id    where        t.teacher_id = tsr.teacher_id) as '学科'from    teacher t
子查询、查询临时表、exists
例子
select    *from    (        select            o.id,            o.student_intention_id,            s. name,            s.area_id,            a.area_name,            s.exam_year,            o. status,            case o. status        when '1' then            '待提交'        when '2' then            '待指派'        when '3' then            '已完成'        when '4' then            '处理中'        end statusname,        case o.emergency_degree    when '1' then        '正常'    when '2' then        '紧急'    when '3' then        '非常紧急'    end emergencydegreename,    o.emergency_degree,    o.update_time,    (        select            first_lesson_time        from            jx_strategy        where            jx_lesson_plan_order_id = o.id        and status in (2, 7)        and first_lesson_time > now()        order by            first_lesson_time asc        limit 1    ) as first_time,    (        select            deal_user_id        from            jx_strategy        where            jx_lesson_plan_order_id = o.id        and status <> 7        and deal_user_id <> 0        order by            id desc        limit 1    ) as deal_user_idfrom    jx_lesson_plan_order oleft join student s on s.student_intention_id = o.student_intention_idleft join area a on s.area_id = a.idwhere    o. status <> 1and s.phone = '18501665888'and o.emergency_degree = 1and o. status = 2and s.exam_year = '2015'and o.update_time >= '2018-08-14 20:28:55'and o.update_time <= '2018-08-14 20:28:55' ) as awhere 1 = 1and a.deal_user_id = 145316and a.first_time >= '2018-08-17 00:00:00'and a.first_time <= '2018-08-30 00:00:00'and exists ( select * from jx_strategy js where js.jx_lesson_plan_order_id = a.id and js. status in (2, 7) and js.subject_id in (2, 3))order by a.update_time desclimit 0, 10
update 关联变量条件修改
1、涉及的表关系: user_info表中的 id_number(身份证号) teacher表中的birth字段、 关联关系usrer_id = teacher_id
2、业务场景:获取用户身份证上的出生日期将出生日期更新在birth字段
update teacher t inner join (select t.teacher_id, t.birth, u.id_number, concat(substring(u.id_number, 7, 4), '-', substring(u.id_number, 11, 2), '-', substring(u.id_number, 13, 2)) as birth1, u.reg_date, t.exit_time from teacher tinner join user_info u on u.user_id = t.teacher_id) info on info.teacher_id = t.teacher_idset t.birth = info.birth1where info.reg_date > '2018-08-20 00:00:00' and info.id_number is not null and (info.birth is null or t.birth = '') and t.is_train = 1
以上就是mysql中关联变量条件修改、查询以及数据显示成一行的介绍的详细内容。
其它类似信息

推荐信息