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

check set of ids in mysql set of ids (each item of firs set, must be in second set)

+-----------+---------+
| InvoiceId | trueIDs |
+-----------+---------+
| ab12345   | 1,2,35  |
| cd1234567 | 92,28,1 |
| asdf12345 | 351,8,1 |
+-----------+---------+
UserAllowed (925,28,1,99,1059,314,422,96,917,356)

Is there any way to compare two sets of id’s?
I want to choose best performance way, to compare between two sets of variable , and if one item of first set exist in table, then return InvoiceId…

There is an array in php, and a comma separated string in db table cell.

If of the array values (UserAllowed), exist in database (trueIDs) return InvoiceId.

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

May be the php array is large, and it isn’t logical to execute the query for each php array item.

SELECT
    Invoices.InvoiceId
FROM
    Invoices 
WHERE
(925,28,1,99,1059,314,422,96,917,356) ???? Invoices.trueIDs

>Solution :

SELECT Invoices.InvoiceId
FROM Invoices 
WHERE JSON_OVERLAPS(CONCAT('[', @UserAllowed, ']'), CONCAT('[', trueIDs, ']'));

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=62fae329e23d3f2885c6b16ce1770765

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