我必须比较两个表,然后将它们全部列为复选框,而匹配的值应该被选中,不匹配的值则不被选中
//表1
rid| role_name
1 | school
2 | college
3 | University
//表2
id|rid | category
1 | 1 | uniform
2 | 2 | uniform
从两个表中匹配去掉,其中类别 = '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='' >"; }
}
关于php - 比较 MYSQL 中的两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826146/