Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Javascript to Extract Data in Given Format

+-------+---------+----------+---------------+-------------+-----+
| serial| amount  | NameOfMyFriends                              |
+-------+---------+----------+---------------+-------------+-----+
|     1 | 1500.00 | FOOBAR1, FOOBAR2, FOOBAR3, FOOBAR4, FOOBAR5, |
|     2 | 2000.00 | FOOBAR1, FOOBAR3, FOOBAR4, FOOBAR5,          |
|     3 |  300.00 | FOOBAR1, FOOBAR2,                            |
|     4 |  600.00 | FOOBAR2, FOOBAR4,                            |
|     5 |  900.00 | FOOBAR4, FOOBAR5,                            |
|     6 |  800.00 | FOOBAR2, FOOBAR5,                            |
|     7 |  600.00 | FOOBAR1, FOOBAR3,                            |
+-------+---------+----------+---------------+-------------+-----+

I need to find each one's share something like this:

  1. FooBar1 's Share : 300 + 500 + 150 + 300 ===> 1250
  2. FooBar2 's Share : 300 + 500 + 150 + 300 + 400 ===> 1650
  3. FooBar3 's Share : 300 + 500 + 300 ===> 800
  4. FooBar4 's Share : 300 + 500 + 300 + 450 + ===> 1550
  5. FooBar5 's Share : 300 + 500 + 450 + 400 ===> 1650

Using JavaScript how can I extract data like this, without using any JavaScript Library?

like image 474
Dharmjeet Kumar Avatar asked Aug 02 '26 10:08

Dharmjeet Kumar


1 Answers

I am answering this as a SQL question because it is tagged MySQL.

This is an awful data format. You should have a separate association table, with one row per serial and friend.

But, if you have a friends table, then you can get what you want using MySQL:

select f.FriendName, sum(eachVal)
from YourTable t join
     Friends f
     on find_in_set(f.FriendName, t.NameOfMyFriends) > 0
group by f.FriendName;
like image 157
Gordon Linoff Avatar answered Aug 04 '26 02:08

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!