我有一个表字段为 (NAME FRUIT TIME)
| NAME | FRUIT | TIME |
|--------|--------|-------|
| AJAY | MANGO | 10:10 |
| SACHIN | APPLE | 12:00 |
| RAJ | MANGO | 10:00 |
| AJAY | MANGO | 12:00 |
| AJAY | MANGO | 11:00 |
| AJAY | APPLE | 12:00 |
| RAJ | BANANA | 12:00 |
| AJAY | BANANA | 12:00 |
| SACHIN | BANANA | 2:00 |
| SACHIN | MANGO | 12:00 |
| RAJ | MANGO | 12:00 |
| SACHIN | APPLE | 12:00 |
| AJAY | APPLE | 12:00 |
| AJAY | APPLE | 12:00 |
现在我想从上表中得到这样的输出:
| NAME | MANGOCOUNT | APPLECOUNT | BANANACOUNT |
|--------|------------|------------|-------------|
| AJAY | 3 | 3 | 1 |
| RAJ | 2 | 0 | 1 |
| SACHIN | 1 | 2 | 1 |
我试过UNION
这样:
SELECT NAME, COUNT(*) AS MANGOCOUNT FROM FRUIT_EAT
WHERE FRUIT='MANGO' GROUP BY NAME
UNION ALL
SELECT NAME, COUNT(*) AS APPLECOUNT FROM FRUIT_EAT
WHERE FRUIT='APPLE' GROUP BY NAME
UNION ALL
SELECT NAME, COUNT(*) AS BANANACOUNT FROM FRUIT_EAT
WHERE FRUIT='BANANA' GROUP BY NAME;
我得到的结果是
NAME MANGOCOUNT
AJAY 3
RAJ 2
SACHIN 1
AJAY 3
RAJ 0
SACHIN 2
AJAY 1
RAJ 1
SACHIN 1
我不明白我的错误。如果你能帮帮我吗?