显示来自不同表的不匹配记录

时间:2014-07-12 11:06:19

标签: php mysql

我有两张表测试 test_new 。我想从两个表的两行显示不匹配的记录。

测试

id | name | age | class | emailid
---------------------------------------
1  | raj  | 24  | 8th   | raj@gmail.com

test_new

id | name | age | class | emailid
---------------------------------------
1  | raj  | 28  | 9th   | raj@gmail.com

当我申请以下代码时。仅显示不匹配记录的计数器,但我还想显示不匹配的行值。

如何显示不匹配的记录?

例如:在上表中,年龄和班级的值不同。我想只显示不匹配的值表示基于emailid的测试表的年龄和类的值。

代码从此处开始:

$result=mysql_query("select * from test_new where emailid='raj@gmail.com'");
$matchvalues=mysql_fetch_array(mysql_query("select * from test where emailid='raj@gmail.com'"));
while($row=mysql_fetch_array($result)) {
    $counter=0;
    foreach($matchvalues as $value) {
        if(!in_array($value, $row)) {
            $counter++;
            print $counter."<br>";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

$result=mysql_query("select test.name as testname,test.age as testage,test.class as testclass,test_new.name as t1name,test_new.age as t1age,test_new.class as t1class from test JOIN test_new on test.emailid=test_new.emailid where test.emailid='raj@gmail.com'");
while($row=mysql_fetch_array($result))
{
if($row['testname']==$row['t1name'])
{
print $row['testname']."<br>";
}
if($row['testage']==$row['t1age'])
{
print $row['testage']."<br>";
}
if($row['testclass']==$row['t1class'])
{
print $row['testclass']."<br>";
}

}

答案 1 :(得分:0)

    while($row=mysql_fetch_array($result)) {
        $counter=0;
        foreach($matchvalues as $value) {

        if($counter%2==1)   
{
            if($value!=$row[$counter]) {
            echo $value."<br>";
                       }
}
                $counter++; 
     }
    }

查看此代码