Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL get rows from table that include a BIGINT specified in a passed-in array

I have recently struck an issue with mysql using serverless-mysql in TypeScript where potentially my query is incorrect? I am using the following to create the query:

export default async function ExcuteQuery(query: any, values: any) {    
    try {        
        const results = await db.query(query, values);
        await db.end();        
        return results;
    } catch (error: any) {
        throw new Error(error.message + error.stack)
    }
  }

let ids: BigInt[] = [222222222222222222222, 3333333333333333333333, 444444444444444444444]
ExcuteQuery("SELECT * FROM servers WHERE id IN (?)", [ids.join()])

Which returned an empty array with no row data inside.

However, when I tried using the query inside phpmyadmin, like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"SELECT * FROM servers WHERE id IN (222222222222222222222, 3333333333333333333333, 444444444444444444444)"

It returned the rows as I would like them, no troubles.
Does this mean I am doing something wrong with the parameter data that I am passing into that query? Is it a type cast issue? I am more then happy to elaborate if that was not clear, just feel free to comment!

I appreciate any responses I get in advance, thank you!

I have attempted to change the parameter data using attempts like:

[ids.join(', ')]
[`(${ids.join()})`]

However, none of those seemed to work either.

>Solution :

SELECT servers.* 
FROM servers
JOIN JSON_TABLE(CONCAT('[', ?, ']'), 
                '$[*]' COLUMNS (id BIGINT PATH '$')) criteria USING (id)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading