简体   繁体   English

比较来自MySQL的两个表

[英]Compare two tables from MYSQL

I have to compare two tables , then list all of them as check boxes, while matched values should be checked and non matching without checked 我必须比较两个表,然后将所有表都列为复选框,同时应检查匹配的值,不检查则不匹配

//table 1 //表格1

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

//table2 // table2

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

match rid from both table where category = 'uniform' list all and checked the matching rid 从category ='uniform'全部列出​​的两个表中匹配掉,并检查匹配掉

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM