0

I want to compare two SQL table( code PHP) : Class1 & Class2

   Class1                      Class2

ID   | Name  | Age           ID  | Name | Age 

101  | Bob   | 20            101 | Bobb | 22
102  | Jame  | 21            102 | Jame | 21 
103  | Kenny | 21 

Suppose, the difference is mistake while typing and Class1 is the original data.

How to hightlight difference in both ( two) table and show this in PHP site. Like that

           Class1                      Class2

ID       |    Name   | Age           ID  | Name      | Age 

101      | **Bob**   | **20**        101 | **Bobb**  | **22**
102      | Jame      | 21            102 | Jame      | 21 
**103**  | **Kenny** | **21** 

My mean is color or bold in PHP code.

Thanks for your help.

2
  • Do you want to compare two databases or two tables ? Commented May 1, 2016 at 20:39
  • Two table. My mistake :( Commented May 1, 2016 at 20:40

1 Answer 1

1

You need to join the two tables using the ID:

SELECT a.*, b.* FROM a LEFT JOIN b USING (id)

and drop identical records

WHERE 
     (a.field1 != b.field1 OR b.field1 IS NULL)
     OR
     (a.field2 != b.field2 OR b.field2 IS NULL)

You can build the WHERE from a foreach() loop on the field names, using implode(' OR ', ...) to join the clauses for each field.

Then you present the records in a table, again with a foreach() loop inside the while($resultset->fetch()) loop, to only highlight the fields that are different.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.