Sergio
Sergio

Reputation: 1239

PHP compare values from Mysql

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

Upvotes: 1

Views: 174

Answers (3)

Sherlock
Sherlock

Reputation: 7597

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)

Upvotes: 3

Sahin Yanlık
Sahin Yanlık

Reputation: 1201

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

Upvotes: 1

roymustang86
roymustang86

Reputation: 8553

You can use array_diff:

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

Upvotes: 2

Related Questions