比较 MYSQL 中的两个表


Compare two tables from MYSQL

我必须比较两个表,然后将它们全部列为复选框,而匹配的值应该被选中,不匹配的值应该被选中,不匹配没有被选中

表1

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

表2

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

从类别 = "统一"的两个表中删除匹配列出所有并检查匹配的 rid

    $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 演示

$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']}";
}