有兩張表,充值和購買。選擇兩張表mysql不加入
select * from recharge;
+-----+------+--------+---------------------+
| idx | user | amount | created |
+-----+------+--------+---------------------+
| 1 | 3 | 10 | 2016-01-09 20:16:18 |
| 2 | 3 | 5 | 2016-01-09 20:16:45 |
+-----+------+--------+---------------------+
select * from purchase;
+-----+------+----------+---------------------+
| idx | user | resource | created |
+-----+------+----------+---------------------+
| 1 | 3 | 2 | 2016-01-09 20:55:30 |
| 2 | 3 | 1 | 2016-01-09 20:55:30 |
+-----+------+----------+---------------------+
我想找出用戶的餘額是SUM(金額) - COUNT(purchase.idx)。 (在這種情況下,13)
所以我曾試圖
SELECT (SUM(`amount`)-COUNT(purchase.idx)) AS balance
FROM `recharge`, `purchase`
WHERE purchase.user = 3 AND recharge.user = 3
但是,它返回的錯誤。
哪裏是錯誤? – Sadikhasan
雖然你的代碼寫得不好,會返回錯誤的結果,但不應該導致錯誤。請明確說明錯誤。 –