旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

mysql中关联变量前提修正、查询以及数据显现成一行的引见【MySQL教程】,mysql

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:57评论:0


导读:本篇文章给人人带来的内容是关于mysql中关联变量前提修正、查询以及数据显现成一行的引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。一对多数据...
本篇文章给人人带来的内容是关于mysql中关联变量前提修正、查询以及数据显现成一行的引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

一对多数据显现成一行

GROUP_CONCAT(expr)

1、触及的表关联:teacher表、teacher_subject_rel表(西席所能教的学科表)、subject表
2、营业场景: 须要拉取一切西席的编号(teacher_no)、学科名(subject_name)。 &nbsp 西席表(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_id
FROM
    jx_lesson_plan_order o
LEFT JOIN student s ON s.student_intention_id = o.student_intention_id
LEFT JOIN area a ON s.area_id = a.id
WHERE
    o. STATUS <> 1
AND s.phone = '18501665888'
AND o.emergency_degree = 1
AND o. STATUS = 2
AND 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 a
WHERE
    1 = 1
AND a.deal_user_id = 145316
AND 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 DESC
LIMIT 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 t
INNER JOIN user_info u ON u.user_id = t.teacher_id

) info on info.teacher_id = t.teacher_id
SET t.birth = info.birth1
WHERE 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中关联变量前提修正、查询以及数据显现成一行的引见的细致内容,更多请关注ki4网别的相干文章!

标签:mysql


欢迎 发表评论: