반응형
질문이 수정되었습니다.
테이블 : 상점
+---------+--------+--------+
| shop_id | name | state |
+---------+--------+--------+
| 0 | shop 0 | 5 |
| 1 | shop 1 | 5 |
| 2 | shop 2 | 5 |
| 3 | shop 3 | 2 |
+---------+--------+--------+
표 : 항목
+------------+--------------+
| shop | item | quantity |
+------------+--------------+
| 0 | 0 | 1 |
| 0 | 1 | 2 |
| 0 | 2 | 3 |
| 1 | 0 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 2 | 0 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 0 | 1 |
| 3 | 1 | 2 |
| 3 | 2 | 3 |
+------------+--------------+
SELECT state,SUM(i.quantity) total
FROM shops s2
LEFT JOIN items i ON i.shop=s2.shopid
WHERE state=5
GROUP by item
result #1:
+--------+---------+
| state | total |
+--------+---------+
| 5 | 3 |
+--------+---------+
| 5 | 6 |
+--------+---------+
| 5 | 9 |
+--------+---------+
But I would like the totals, like this:
result #2:
+--------+---------+---------+----------+
| state | total 0 | total 1 | total 2 |
+--------+---------+---------+----------+
| 5 | 3 | 6 | 9 |
+--------+---------+---------+----------+
or using group_concat()
result #3
+--------+---------+
| state | totals |
+--------+---------+
| 5 | 3,6,9 |
+--------+---------+
결과 # 1에서 총 열을 가져 오기 위해 group_concat을 얻을 수없는 것 같습니다.
미리 감사드립니다
해결 방법
이를 수행하는 방법을 찾았습니다.
SELECT state,GROUP_CONCAT(cast(total as char))
FROM
(
SELECT state,SUM(i.quantity) total
FROM shops s
LEFT JOIN items i ON i.shop=s.shopid
WHERE state=5
GROUP by item
) s
참조 페이지 https://stackoverflow.com/questions/2228307
반응형
'MySql' 카테고리의 다른 글
MySQL Export MySQL data to .csv using PHP (0) | 2020.12.09 |
---|---|
MySQL Any good relational database tutorials? (0) | 2020.12.09 |
MySQL 하나의 값이 다른 테이블에없는 경우 테이블에 삽입 하시겠습니까? (0) | 2020.12.09 |
MySQL 경고 : mysqli_query ()는 매개 변수 1이 주어진 mysqli 부울이 될 것으로 예상합니다. (0) | 2020.12.09 |
MySQL 현재 GMT 시간에 대한 MySQL 쿼리 (0) | 2020.12.09 |
댓글