-1

I was wondering if what is this called or is this even possible in MySql, or what should I do to achieve this. This is what Mysql Table Looks Like:

enter image description here

Desired Output:

enter image description here

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • please don't post images of data see https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question and this is also interesting https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – nbk Jul 30 '21 at 17:37
  • I just snipped it from Excel, I was also posting table related problem but always get votes down for being vague, also the link refers to posting code as images and I haven't done that, anyway thanks for the heads up and I will be more mindful and careful now in my questions Formatting, have a nice day nbk. – Akagami Shanks Aug 01 '21 at 07:25

2 Answers2

0

Your table structures wont allow (anything near as easy). A better format of your table might be more like

id   customerid   item      qty
1    15           Car       7  
2    15           Car       2
3    15           Engine    3
4    15           Engine    2
5    15           Exhaust   5

Then, you could get what you are looking for easily such as

select
      t.customerid,
      t.item,
      sum( t.qty )
   from
      YourTable t
   group by
      t.customerid,
      t.item

This WOULD give your your

customerid  item      qty
15          Car       9
15          Engine    5
15          Exhaust   5

Having a properly formatted table vs trying to have a description requires parsing numeric values vs rest of description is never a good solution such as what you have here. Too many opportunities for bad answers.

DRapp
  • 47,638
  • 12
  • 72
  • 142
0

you can use a query like this:

SELECT  customerid 
 , CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
FROM myitems
GROUP BY SUBSTRING_INDEX(item, ',', -1);

Sample

MariaDB [bernd]> SELECT * FROM myitems;
+----+------------+---------------+
| id | customerid | item          |
+----+------------+---------------+
|  1 |         15 | 7pcs, Car     |
|  2 |         15 | 2pcs, Car     |
|  3 |         15 | 3pcs, Engine  |
|  4 |         15 | 2pcs, Engine  |
|  5 |         15 | 5pcs, Exhaust |
+----+------------+---------------+
5 rows in set (0.07 sec)

MariaDB [bernd]> SELECT customerid 
    ->      , CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
    ->     FROM myitems
    ->     GROUP BY SUBSTRING_INDEX(item, ',', -1);
+------------+---------------+
| customerid | item          |
+------------+---------------+
|         15 | 9pcs, Car     |
|         15 | 5pcs, Engine  |
|         15 | 5pcs, Exhaust |
+------------+---------------+
3 rows in set, 5 warnings (0.02 sec)

MariaDB [bernd]> 
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
  • change **GROUP BY SUBSTRING_INDEX(item, ',', -1);** to **GROUP BY customerid, SUBSTRING_INDEX(item, ',', -1);** if you have more then one customer in your table – Bernd Buffen Jul 30 '21 at 17:46
  • Thank you for Helping a noob like me, this was perfect. I only have one customer as this is a sub table to a main table, thank you very much Bernd. – Akagami Shanks Jul 30 '21 at 17:49