I am using the leftjoin table, but the result does not seem to be the same as that of SELECT {code ...}.
I use left join to join the table itself, but the result does not seem to be correct.
SELECT
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
Reply content:
What is quick writing?
I use left join to join the table itself, but the result does not seem to be correct.
SELECT
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
Assume that the following data table is available:tbl
| Uid |
S |
Date |
| 1 |
5 |
2016-08-31 |
| 2 |
3 |
2016-08-31 |
| 3 |
7 |
2016-08-31 |
| 1 |
2 |
|
| 2 |
5 |
|
| 4 |
4 |
|
Run
SELECT today.uid, today.s - IFNULL(yesterday.s, 0) AS diffFROM (SELECT uid, SUM(s) AS s FROM tbl WHERE date='2016-08-31' GROUP BY uid) AS todayLEFT OUTER JOIN (SELECT uid, SUM(s) AS s FROM tbl WHERE date='2016-08-30' GROUP BY uid) AS yesterdayUSING (uid)ORDER BY diff DESC;
Result