2016-01-13 62 views
-3

我有3個表
選擇MYSQL出席

**User**<br> 
**ID | name     | npm  | dept | created** 
    1 | TOTOK HERDIYANTO  | NULL  | 24 | 2016-01-12 09:05:13 
    5 | BIMA BRAHMANA   | NULL  | 24 | 2016-01-12 09:05:13 
    7 | IDA FARIDA    | NULL  | 24 | 2016-01-12 09:05:13 
30 | AHMAD MAULANA   | NULL  | 23 | 2016-01-12 09:05:13 
47 | DINI GANDINI PURBANINGR| NULL  | 24 | 2016-01-12 09:05:13 
58 | ANWAR JAYANINGRAT  | NULL  | 23 | 2016-01-12 09:05:13 
86 | Yudia Natakusuma  | NULL  | 24 | 2016-01-12 09:05:13 
96 | DENNY GANJAR P   | NULL  | 23 | 2016-01-12 09:05:13 
106 | NOFI FIRDIYARTI  | NULL  | 24 | 2016-01-12 09:05:13 
108 | RIAMA MILA THERESIA | NULL  | 22 | 2016-01-12 09:05:13 
118 | PRATIWI KUSUMA DEWI | NULL  | 24 | 2016-01-12 09:05:13 
126 | Shindi Azalika Nur Fala| 08.1014.022| 22 | 2016-01-12 9:05:13 
128 | M. Bayu Edigani Putra | NULL  | 22 | 2016-01-12 09:05:13 
143 | Ardini Yati   | 08.0914.020| 22 | 2016-01-12 09:05:13 
150 | Dhenaldi Savirio Firman| NULL  | 22 | 2016-01-12 09:05:13 
153 | Rakha Prisma Ardyan | NULL  | 22 | 2016-01-12 09:05:13 
154 | Trisna Darapuspa  | NULL  | 22 | 2016-01-12 09:05:13 

**Department** 
**ID | name** 
    25 | Finance 
    23 | ICT 
    22 | Marketing 
    24 | SDM, Umum dan Legal 

**attendance** 
**userID | date  | in  | out** 
    1 | 2015-01-12 | 09:05:13 | 17:30:02 
    7 | 2015-01-12 | 09:05:13 | 17:30:02 
    30 | 2015-01-12 | 09:05:13 | 17:30:02 
    47 | 2015-01-12 | 09:05:13 | 17:30:02 
    58 | 2015-01-12 | 09:05:13 | 17:30:02 
    86 | 2015-01-12 | 09:05:13 | 17:30:02 
    106 | 2015-01-12 | 09:05:13 | 17:30:02 
    108 | 2015-01-12 | 09:05:13 | 17:30:02 
    118 | 2015-01-12 | 09:05:13 | 17:30:02 
    126 | 2015-01-12 | 09:05:13 | 17:30:02 
    128 | 2015-01-12 | 09:05:13 | 17:30:02 
    143 | 2015-01-12 | 09:05:13 | 17:30:02 
    150 | 2015-01-12 | 09:05:13 | 17:30:02 
    153 | 2015-01-12 | 09:05:13 | 17:30:02 
    154 | 2015-01-12 | 09:05:13 | 17:30:02 

我想我的結果,如:

**where** 
date = 2015-01-12 // yyy-mm-dd 
deptid = in(23,24) 

**RESULT** 
**userID | date  | in  | out** 
    1 | 2015-01-12 | 09:05:13 | 17:30:02 
    5 | 2015-01-12 | 
    7 | 2015-01-12 | 09:05:13 | 17:30:02 
    47 | 2015-01-12 | 09:05:13 | 17:30:02 
    58 | 2015-01-12 | 09:05:13 | 17:30:02 
    86 | 2015-01-12 | 09:05:13 | 17:30:02 
    96 | 2015-01-12 | 
    106 | 2015-01-12 | 09:05:13 | 17:30:02 
    118 | 2015-01-12 | 09:05:13 | 17:30:02 
    153 | 2015-01-12 | 09:05:13 | 17:30:02 
    154 | 2015-01-12 | 09:05:13 | 17:30:02 
+0

出勤和用戶表之間沒有關係,規範表正常出勤和用戶表之間的關係 –

+0

是user.ID = attendance.userID –

回答

0

該查詢會給你輸出提及。

select a.userID, a.date, a.in, a.out from attendance a 
inner join user u on u.ID = a.userID 
inner join Department d on u.dept = d.ID 
where d.ID in (23,24) and u.date = '2015-01-12' 

感謝 阿米特

+0

不需要這樣:'感謝Amit' –

+0

日期用戶只是時間戳不能用於日期 –

1

的結果集必須不包含這兩行:

5 | 2015-01-12 | | 
96 | 2015-01-12 | | 

因爲不符合你提到的標準(相關用戶不出席存在表)。除非您將標準更改爲:

where 
date = 2015-01-12 // yyy-mm-dd 
    **OR** 
deptid = in(23,24) 
0

嘗試LEFT JOIN以獲得所需結果。像下面..

SELECT 
    u.ID, 
    a.date, 
    a.in, 
    a.out 
FROM User AS u 
    LEFT JOIN attendance AS a 
    ON u.ID = a.userID 
    AND a.date = '2015-01-12' 
    AND u.dept IN (23,24)