1

In a database i have two colums: id and value. I want to check how many difrent id's there is in my result, how can i do that?

I have tried to think of an solution, but i haven't succeed more that countDifferentId(){// do some magic calculations}

result looks like this:

[ RowDataPacket {
    id: 76561198115203520,
    value: 73 },
  RowDataPacket {
    id: 76561198115029751,
    value: 73 }
  RowDataPacket {
    id: 76561198115702984,
    value: 73 },
  RowDataPacket {
    id: 76561198115203520,
    value: 73 } ]

So the result of countDifferentId() should be 3

3 Answers 3

3

If you want to do the job on javascript side, here you go:

const rowDataPackets = [
    {
        id: 76561198115203520,
        value: 73
    },
    {
        id: 76561198115029751,
        value: 73
    },
    {
        id: 76561198115702984,
        value: 73
    },
    {
        id: 76561198115203520,
        value: 73
    }
];


function countDifferentId(array){
    // Object with key=id to count unique ids and value is true (or whatever)
    const uniqueIds = array.reduce((acc, data) => Object.assign(acc, {[data.id]:true}) , {});
    // Number of unique ids is the number of keys
    return Object.keys(uniqueIds).length;
}

console.log(countDifferentId(rowDataPackets));
Sign up to request clarification or add additional context in comments.

3 Comments

This worked! Very compact and fast, just what i needed
Note: i changed const uniqueIds = rowDataPackets.reduce to const uniqueIds = array.reduce
Good catch! happy it helps :)
2

Don't you want to do so in a group by query? There're lots of examples here. e.g.

https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-count-with-group-by.php

MySQL Query with count and group by

etc.

If you have only server request from API, you may want . to create a hash table like { id: count }

7 Comments

I hoped for a JS solution as i think it is faster for me
JS is not necessarily faster, it's often better share work between servers. Group by is a good option. If there's a lot of rows to fetch and you just want to count, even better do a select count(distinct id) from...
@RaphaMex the rows will not exceed 100 and i need to make a call every 1 second, is it still better?
I made a pure JS solution, if you want to give it a try and compare. 100 rows every second, that's not a problem for either solution. I was just commenting in case you had thousands of rows, better relieve your network.
i agree that doing this at the query level is ideal, but sometimes its not a viable option depending on project dynamics.
|
1

function grabs all values from one array column and returns as an array.

then for loop uses an array to store counted values, checking the array each time to ensure values counted only once.

function getCol(myarray, col){ 
    var column = []; 
    for(var i=0; i<myarray.length; i++){ 
        column.push(myarray[i][col]); 
    } 
    return column; 
    }

    cols = getCol(result, 'id');
    var count = 0;
    var used = [];
    for(var i = 0; i<cols.length;i++){
         if(!used.indexOf(cols[i])){
               used.push(cols[i]);
               count++;
         }
     }

9 Comments

Can you please explain your answer?
crappy stack app posted this twice ugh
Indeed it is crap
If i wanted to call getCol() how would i parse the MySQL result into the function?
getCol(result,'id');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.