3

I have a simple array with some values:

$firstArray = array (
  ['val1'] => 'a',
  ['val2'] => 'b',
  ['val3'] => 'c'
);

and I would like to find how many values of this array appear in every line of a MySQL table like this one:

| ID |  Name  |    Val    |
| 01 | voice1 | a,c,d,e   |    // it should return 2 (a and c)
| 02 | voice2 | a,b,c,d,f |    // it should return 3 (a,b and c)

The best thing should be to have an array with ID, name, and how many values.

1 Answer 1

2

You can use the FIND_IN_SET that will return you the position of the matched value and convert it to 1 or 0 with the SIGN function.

select
    SIGN(FIND_IN_SET('a', texte)) +
    SIGN(FIND_IN_SET('b', texte)) +
    SIGN(FIND_IN_SET('c', texte))
from your_table;

This returns exactly what you expected with your test data-set.

Regards,

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you and sorry for the delay!
Sorry... a part of the question was "The best thing should be to have an array with ID, name, and how many values"... how to obtain it?
You just have to add: "ID, name, " just after the select.

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.