0

I have a MySQL table like this fiddle here: http://sqlfiddle.com/#!2/4ae5c/15

And the other has different from an API: http://sqlfiddle.com/#!2/b7946e/1

What I want to have is a result (weekly) like this:

+----------+----------+----------+------------+------------+------------+
| date     | action   | position | name       | day        | ID         |
+----------+----------+----------+------------+------------+------------+
|17.06.2013| holiday  | home     | Winkler,Mr.| Monday     | 0          |
|18.06.2013| holiday  | home     | Winkler,Mr.| Tuesday    | 1          |
|19.06.2013| work     | NYC      | Winkler,Mr.| Wednesday  | 2          |
|20.06.2013| work     | NYC      | Winkler,Mr.| Thursday   | 3          |
|21.06.2013| work     | NYC      | Winkler,Mr.| Friday     | 4          |
|22.06.2013| work     | NYC      | Winkler,Mr.| Saturday   | 5          |
|23.06.2013| weekend  | home     | Winkler,Mr.| Sunday     | 6          |
+----------+----------+----------+------------+------------+------------+

Does anyone of you have experience with anything like this?

I do not get do merge them correctly because of the missing dates in the second table.

4

1 回答 1

2

Try this one i have joined your tables and noticed that DATETIME field having type text which is too bad to save the date as text this should be as date or datetime but however i have used STR_TO_DATE to convert as date , In below query i have used the left join for mydates and also used CASE to make custom conditions , you have to provide one date so to get the records of WEEK for that date

SELECT md.`mydate`,
(CASE WHEN g.`ACTION` IS NULL  &&  DAYNAME(md.`mydate`) ='Sunday'  THEN 'Weekend' 
WHEN g.`ACTION` ='0' THEN g.`ACTIONTEXT` 
WHEN  g.`ACTION` IS NULL THEN 'holiday' 
ELSE 'holiday' END) `action` 
,
(CASE WHEN g.`POSITION` IS NULL THEN 'Home' ELSE g.`POSITION` END)`position` ,
g.`DRIVERNAME`,DAYNAME(md.`mydate`),g.`ID`  FROM `mydates` md 
LEFT JOIN `geoimportroot` g ON (md.`mydate`=  STR_TO_DATE(g.`DATETIME`, "%d.%m.%Y")  )
WHERE WEEK(md.`mydate`)= WEEK('2013-06-17')

Here is your fiddle

I hope this is what you are looking for

于 2013-09-27T10:59:42.737 回答