Answer the question
In order to leave comments, you need to log in
MySQL - how to write a query with multiple SELECTs?
Let's say there is a table TBL1
box_name | fruit_name |
box1 | apple |
box1 | orange |
box2 | apple |
box1 | tomato |
box2 | tomato |
box_name | total_fruit | fruit_apple | fruit_orange | fruit_tomato |
box1 | 3 | one | one | one |
box2 | 2 | one | 0 | one |
Answer the question
In order to leave comments, you need to log in
SELECT
box_name,
SUM(CASE WHEN (fruit_name='apple') THEN 1 ELSE 0 END) AS fruit_apple,
SUM(CASE WHEN (fruit_name='orange') THEN 1 ELSE 0 END) AS fruit_orange,
.... и тд
FROM
your_table
GROUP BY
box_name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question