글을 빨리 쓰는 방법은 무엇인가요
이제 테이블 자체에 Left Join을 사용하고 있는데 결과가 잘못된 것 같습니다
선택
<code> table.id ,SUM(table.s-yestoday.a) as sum FROM table LEFT JOIN table yestoday ON yestoday.uid = wy_appdata.uid WHERE table.year = '.$year.' AND table.month = '.$month.' AND table.day = '.$day.' AND yestoday.year = '.$bre_data['year'] .' AND yestoday.month ='.$bre_data['month'] .' AND yestoday.day = '.$bre_data['day'] .' GROUP BY table.uid ORDER BY sum DESC</code>
글을 빨리 쓰는 방법은 무엇인가요
이제 테이블 자체에 Left Join을 사용하고 있는데 결과가 잘못된 것 같습니다
선택
<code> table.id ,SUM(table.s-yestoday.a) as sum FROM table LEFT JOIN table yestoday ON yestoday.uid = wy_appdata.uid WHERE table.year = '.$year.' AND table.month = '.$month.' AND table.day = '.$day.' AND yestoday.year = '.$bre_data['year'] .' AND yestoday.month ='.$bre_data['month'] .' AND yestoday.day = '.$bre_data['day'] .' GROUP BY table.uid ORDER BY sum DESC</code>
다음과 같은 데이터 테이블이 있다고 가정해보자tbl
uid | s | date |
---|---|---|
1 | 5 | 2016-08-31 |
2 | 3 | 2016-08-31 |
3 | 7 | 2016-08-31 |
1 | 2 | 2016-08-30 |
2 | 5 | 2016-08-30 |
4 | 4 | 2016-08-30 |
달려
<code class="sql">SELECT today.uid, today.s - IFNULL(yesterday.s, 0) AS diff FROM (SELECT uid, SUM(s) AS s FROM tbl WHERE date='2016-08-31' GROUP BY uid) AS today LEFT OUTER JOIN (SELECT uid, SUM(s) AS s FROM tbl WHERE date='2016-08-30' GROUP BY uid) AS yesterday USING (uid) ORDER BY diff DESC;</code>
결과
uid | diff |
---|---|
3 | 7 |
1 | 3 |
2 | -2 |