eaa_schedule_user是記錄使用者是否已讀的(中間)表,base_v_user_group_department是使用者(檢視)表,其中readtime為null視為未讀,readtime不為null視為已讀,base_v_user_group_department中userorder是使用者的排序
1、未讀排在前面,已讀排在後面;2、使用者按照userorder公升序排序;
1、mysql字段值為null,值最小,所以使用公升序排序;
2、未讀和已讀分開查詢,然後使用union關鍵字連線查詢結果;
3、內部使用left join左連線查詢,eaa_schedule_user作為左表,base_v_user_group_department為右表;
select * from (
(select
b.departmentname,
b.groupname,
b.`name` as readusername,
b.userorder,
a.readtime,
0 as od
from
eaa_schedule_user as a
left join base_v_user_group_department as b on a.userid = b.id
where 1 = 1
and ( a.scheduleid = '')
and ( b.departmentname != null or b.departmentname != '')
and a.readtime is null
order by b.userorder asc)
union
(select
b.departmentname,
b.groupname,
b.`name` as readusername,
b.userorder,
a.readtime,
1 as od
from
eaa_schedule_user as a
left join base_v_user_group_department as b on a.userid = b.id
where 1 = 1
and ( a.scheduleid = '')
and ( b.departmentname != null or b.departmentname != '')
and a.readtime is not null
order by b.userorder asc)
) as a
where 1 = 1
order by a.od asc,a.userorder asc
mysql 左連線 重複 mysql左連線重複行
我正在設計乙個類似於4個 的字典資料庫 words wordid lemma senses wordid synsetid synsets synsetid definition samples synsetid sample所以我使用下面的查詢來獲取所有意義定義和樣本 if isset searc...
mysql左聯一對多 MySQL左連線多對一行
為了簡化我的問題 假設我有3個表.rooms people things id name id name fk rooms id name fk rooms 1 kitchen 1 john 1 1 tv 2 2 bedroom 2 mary 2 2 bed 2 3 andy 1 3 sink 1 ...
mysql多表連線
現有表r,s如下 select from r,s 結果 注 不需要任何條件。結果為兩張表函式相乘 3x3 9 自連線 select e.empno,e.ename,m.empno,m.ename from emp e,emp m where m.mgr e.empno 分為三種 交叉連線 內連線 外...