1

The MySql table looks like:

    id    |        values   
  --------------------------------
    41    |    154, 256, 526, 50
    86    |    542, 586, 785, 41

I want to compare values ​​from this database with values ​​from results from another MySql query. From that another query I get result for specific user and valus, for example:

user id: 41
values: 154,526,50

How can I compare this two database "values" columns and get as a result number of total different values ​​and the values ​​that are different?

In this example the result of that PHP function will be:

total number of different values: 1
different value: 256
2
  • 3
    What have you tried? Commented Jul 9, 2012 at 12:59
  • Did you try exploding both fields and comparing arrays? Commented Jul 9, 2012 at 12:59

3 Answers 3

3

By normalising your database. Save those values in a different table and create a new couplings table, connecting the values to the user. This will ease your development dramatically.

EDIT: The query would then look like this:

SELECT
    value
FROM
    couplings_table
WHERE
    user_id = X
AND
    value NOT IN (SELECT
                    value
                  FROM
                    couplings_table
                  WHERE
                    user_id = Y)
Sign up to request clarification or add additional context in comments.

1 Comment

Please show what the query would look like in a normalized database design.
2

You can use array_diff:

http://php.net/manual/en/function.array-diff.php

Comments

1

explode with "," and use array diff or search every item in_array and use results. I think array_diff is just for this.

Comments

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.