我试图通过以下查询来获取我想要的所有信息。那是status = 0
和SUM点数status = 0
和status = 1
哪里的评论值。以下是我已经得到了到现在为止(我不能抢在这一点上的评论值)MySQL单个查询多个结果
SELECT
IF(status = 0, comment, NULL) AS com,
SUM(IF(status = 0, points, 0)) AS points1,
SUM(IF(status = 1, points, 0)) AS points2
FROM `tablename`
WHERE mid = $mid
AND stage = 0
表数据:
+----+--------+--------+-----------+-----+------+
| id | mid | points | comment |stage|status|
+----+--------+--------+-----------+-----+------+
| 1 | 500 | 15 | Text here | 0 | 0 |
| 2 | 500 | 5 | Blablabla | 0 | 1 |
| 3 | 20 | 7 | | 1 | 0 |
| 4 | 356 | 10 | More text | 0 | 2 |
| 5 | 9 | 0 | | 1 | 0 |
| 6 | 52 | 5 | Text etc | 0 | 1 |
| 7 | 520 | 13 | Texting | 1 | 0 |
| 8 | 540 | 8 | | 0 | 0 |
+----------------------------------------+------+
结果我要找:
- 其中
mid = 500
和stage = 0
- 如果
status = 0
得到我的积分(15
在这种情况下) - IF
status = 1
让我(在这种情况下5
)点 - IF
status = 0
让我(在这种情况下Text here
)评论
编辑您的问题,并提供样本数据和期望的结果。 –
@GordonLinoff这是更好吗? – Bill