比较快的写法是什么
我现在用left join 这个表本身,但是结果好像不对
SELECT
<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 这个表本身,但是结果好像不对
SELECT
<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 |
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号