简体   繁体   中英

How to write nested query MySQL PHP

I'm developing a page to edit board meetings and I want to display all board members who did not attend specific meeting as a checkox located below who attend as an edit in case of user want to add more so I did this:

My code:

$q = "SELECT * FROM `boardteam`";
    $r = mysql_query($q);
    while ($dbfield = mysql_fetch_assoc($r))
    {
        $member_id =$dbfield['nationalID'];
        $query = "SELECT `attendance` FROM `meetingattendance` WHERE `meetingID` = '$mid' AND `attendance`!= '$member_id'";
    $res = mysql_query($query);
    if ($res)
        {
        $tname ="";
        switch ($dbfield['titleName'])
        {
            case "Dr":
            $tname .= "د.";
            break;
            case "Ms":
            $tname .= "السيدة.";
            break;
            case "Mr":
            $tname .= "السيد.";
            break;
        }
        $At .= "<input type='checkbox' name='moreAttendence[]' dir='rtl' value=".$dbfield['nationalID']."><div class='styled-checkbox'>".$tname." ".$dbfield['fName']." ".$dbfield['sName']." ".$dbfield['lName']."</div><br>";
        }

    }

DB:

CREATE TABLE `boardteam` (
`nationalID` int(10) NOT NULL,
`titleName` char(2) NOT NULL,
`fName` char(20) NOT NULL,
`sName` char(20) NOT NULL,
`lName` char(20) NOT NULL,
`gender` char(1) NOT NULL,
`birthDate` date DEFAULT NULL,
`materialStatus` char(15) DEFAULT NULL,
`jobTitle` varchar(100) NOT NULL,
`jobLocation` varchar(20) DEFAULT NULL,
`employer` varchar(100) DEFAULT NULL,
`email` varchar(100) NOT NULL,
`photo` varchar(255) DEFAULT NULL,
`academicGrade` char(15) DEFAULT NULL,
`employmentStartDate` date NOT NULL,
`employmentEndDate` date NOT NULL,
`employmentType` char(20) DEFAULT NULL,
`employmentStatus` char(15) DEFAULT NULL,
`jobStartDate` date DEFAULT NULL,
`jobNumber` int(10) DEFAULT NULL,
`cv` varchar(255) DEFAULT NULL,
PRIMARY KEY (`nationalID`)
)


CREATE TABLE `meetingattendance` (
 `meetingID` int(11) NOT NULL,
 `attendance` int(10) DEFAULT NULL,
 `absence` int(10) DEFAULT NULL,
 `reason` varchar(255) DEFAULT NULL,
 `additionalAttendance` varchar(255) DEFAULT NULL,
 KEY `absence` (`absence`),
 KEY `meeingID` (`meetingID`),
 KEY `attendance` (`attendance`),
 CONSTRAINT `meetingattendane_ibfk_1` FOREIGN KEY (`meetingID`) REFERENCES `boardmeetings` (`meetingID`),
 CONSTRAINT `meetingattendane_ibfk_2` FOREIGN KEY (`attendance`) REFERENCES `boardteam` (`nationalID`),
 CONSTRAINT `meetingattendane_ibfk_3` FOREIGN KEY (`absence`) REFERENCES `boardteam` (`nationalID`)
)

With my code I got all board members including who attend, How to fix that ??

You need to use a LEFT JOIN in order to find people in the boardTeam who were not in a specific meeting. eg:

SELECT b.*, m.attendance
  FROM boardTeam b
    LEFT JOIN meetingattendance m
      ON b.nationalID = m.attendance AND m.meetingID = $mid
    WHERE m.meetingID IS NULL

If you want to get ALL board members, and then determine within PHP if they attended the meeting or not, simply remove the m.attendance IS NULL clause, as such:

SELECT b.*, m.attendance as attendance
  FROM boardTeam b
    LEFT JOIN meetingattendance m
      ON b.nationalID = m.attendance AND m.meetingID = $mid

and now when you loop through the response rows in php, you can test as such (assuming you fetch your rows one by one into a $row variable):

if($row['attendance'] != null)
{
  // attended meeting
}
else
{
  // did not attend meeting
}

Also, as mentioned in the comments, use mysqli, or pdo instead of pure mysql_ functions

Example fiddle here: http://sqlfiddle.com/#!9/ba7d4/6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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