比较MYSQL中的两个表

时间:2013-09-16 10:47:28

标签: php mysql sql sqlite

我必须比较两个表,然后将它们全部列为复选框,同时应检查匹配的值并且不进行匹配而不匹配

//表1

rid| role_name  
1  |    school  
2  |    college  
3  |  University  

// table2

id|rid | category  
1 | 1  |  uniform  
2 | 2  |  uniform

匹配从两个表中删除category ='uniform' 列出所有并检查匹配的rid

2 个答案:

答案 0 :(得分:0)

$query = "select a.* from table1 a, table b where a.rid = b.rid and a.category = b.category";

$result = mysqli->query($query);

$fetched = $result->fetch_assoc();

foreach($fetched as $row){
echo "<input type='checkbox' name='{$row['rid']}' value='' checked > {$row['category']}";
}

答案 1 :(得分:0)

    $query = "select t1.rid, 'matched' as matching from table1 t1 where t1.rid  in (
              select rid from table2 where category = 'uniform')
             union
             select t1.rid ,'notmatched' from table1 t1 where t1.rid not in (
             select rid from table2 where category = 'uniform')";
    $result = mysqli->query($query);
    $fetched = $result->fetch_assoc();
    foreach($fetched as $row){
    if($row['matching'] = 'matched'){
           echo "<input type='checkbox' name='{$row['rid']}' value='' checked='checked' >"; }
    else{ 
            echo "<input type='checkbox' name='{$row['rid']}' value=''  >"; }
     }

SQL DEMO

相关问题