标签:asc cas left join main when 单位 ase group by number
# 查询主表相关信息,并且显示最新的记录
SELECT
type.number as ‘序号‘,
type.date as ‘时间‘,
type.name as ‘名称‘,
main.content as ‘项目名称‘,
main.manager as ‘负责人‘,
unit.name as ‘单位‘,
re.content as ‘记录‘,
CASE main.status
WHEN 1 THEN ‘完成‘
WHEN 2 THEN ‘正常‘
WHEN 3 THEN ‘滞后‘
END as ‘状态‘
FROM
`main`
LEFT JOIN type ON main.type_id= type.id
LEFT JOIN unit ON unit.id = main.qtdw
# 这里将子查询的结果作为表与主表链接。因为主表与记录表是一对多的关系,故先将结果按时间倒叙查询出来,再将查询的结果分组,这样每个组取到的就是最新的消息了
LEFT JOIN (SELECT * FROM (SELECT pid,content FROM record ORDER BY created_at DESC) a GROUP BY pid) re ON wj.pid = main.id
WHERE
main.status in (2,3)
AND (
1 = 1
AND main.type_id IN
)
AND `recovery_status` = 1
ORDER BY
type.date DESC,
type.number DESC,
main.sort+ 0 ASC
标签:asc cas left join main when 单位 ase group by number
原文地址:https://www.cnblogs.com/Aienming/p/14388130.html