我必须比较两个表,然后将所有表都列为复选框,同时应检查匹配的值,不检查则不匹配

//表格1

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


// table2

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


从category ='uniform'的两个表中匹配
全部列出并检查匹配的摆脱

最佳答案

    $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

关于php - 比较来自MySQL的两个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18826146/

10-13 01:44