이전 질문에서 모든 학기의 모든 과목을 하나의 레코드로 병합하여 각 학생이 하나의 응답이 되도록 하는 방법에 대해 질문했습니다. 그의 데이터는 다음 예와 같습니다.
<code class="json">[ { "userid": "1", - from users table "username": "joe", - from users table "subjectsid": "1", - first subject id for the student in this case the one for phy "subjectname": "bio", - current subject name "activepts": "652", - points of current month "totalpts": "717", - total points of all subjects for this student "sem1": "32", - total points of all subjects for this student of semester 1 "sem2": "0", - total points of all subjects for this student of semester 2 "sem3": "685", - total points of all subjects for this student of semester 3 } ]</code>
그래서 저는 단 하나의 답을 얻었지만 그 답은 어떻게 해야 하는지는 알려주지 않았는데 단지 LEFT JOIN을 사용해야 하는 이유만 알려주었을 뿐입니다. 제 경우에는 LEFT JOIN이 무엇을 할 것인지 설명해주세요. 그래서 제가 해야 할 일을 다시 설명하고 코드와 몇 가지 예를 통해 이를 수행하는 방법을 설명하겠습니다.
1- 각 학생은 하나의 응답이 됩니다
2- 각 응답에는 모든 주제가 포함되고 각 주제는 행으로 구성됩니다
3- 첫 번째 행에는 학생에 대한 일부 정적 데이터가 포함됩니다.
4- 나머지 행은 DB에서 가져온 것과 동일하므로 따로 입력할 필요가 없습니다. 주제를 이 사용자에게만 필터링하는 것을 제외하고 변경
<code class="php">$sql = 'SELECT subjects.userid, users.name AS username, ( SELECT id FROM tbsubjects WHERE userid = subjects.userid ORDER BY id ASC LIMIT 1 ) AS subjectsid, ( SELECT name FROM tbsubjects WHERE userid = subjects.userid ORDER BY time DESC LIMIT 1 ) AS subjectname, ( SELECT IFNULL(SUM(points), 0) FROM tbsubjects WHERE userid = subjects.userid AND month = DATE_FORMAT(NOW(), "%c") ) AS activepts, IFNULL(SUM(subjects.points), 0) AS totalpts, ( SELECT IFNULL(SUM(points), 0) FROM tbsubjects WHERE userid = subjects.userid AND semester = 1 ) AS sem1, ( SELECT IFNULL(SUM(points), 0) FROM tbsubjects WHERE userid = subjects.userid AND semester = 2 ) AS sem2, ( SELECT IFNULL(SUM(points), 0) FROM tbsubjects WHERE userid = subjects.userid AND semester = 3 ) AS sem3 FROM tbsubjects AS subjects LEFT JOIN tbusers AS users ON users.id = subjects.userid WHERE subjects.userid = :userid'; $bindings = array( ':userid' => $userID, ); $users = $statement->fetchAll(PDO::FETCH_OBJ);</code>
코드 설명:
1- 필드 SELECT 문에서 볼 수 있듯이 두 그룹으로 나누어져 있습니다. 맨 위에 있는 첫 번째 그룹은 학생을 위한 정적 필드이고 나머지는 테이블 tbsubjects에서 값을 가져옵니다
2- LEFT JOIN 함수는 가져오는 작업을 담당합니다. tbsubjects 테이블의 모든 (userid)를 userid 사이의 공통 키로 병합합니다. 즉, 이제 각 사용자는 첫 번째 주제(ORDER BY id ASC )와 다른 모든 주제를 포함하는 최소한 하나의 행으로 표시됩니다. 이미 한 행에 병합된 내용은 학생의 정적 필드로서 나머지 행에서 반복됩니다.
<code class="json">[ { "userid": "1", - from users table "username": "joe", - from users table "subjectsid": "1", - first subject id for the student in this case the one for phy "subjectname": "bio", - current subject name "activepts": "652", - points of current month "totalpts": "717", - total points of all subjects for this student "sem1": "32", - total points of all subjects for this student of semester 1 "sem2": "0", - total points of all subjects for this student of semester 2 "sem3": "685", - total points of all subjects for this student of semester 3 }, { "userid": "1", - from users table "username": "joe", - from users table "subjectsid": "1", - first subject id for the student in this case the one for phy "subjectname": "phy", - current subject name "activepts": "10", - points of current month "totalpts": "717", - total points of all subjects for this student "sem1": "32", - total points of all subjects for this student of semester 1 "sem2": "0", - total points of all subjects for this student of semester 2 "sem3": "685", - total points of all subjects for this student of semester 3 }, { "userid": "1", - from users table "username": "joe", - from users table "subjectsid": "1", - first subject id for the student in this case the one for phy "subjectname": "math", - current subject name "activepts": "33", - points of current month "totalpts": "717", - total points of all subjects for this student "sem1": "32", - total points of all subjects for this student of semester 1 "sem2": "0", - total points of all subjects for this student of semester 2 "sem3": "685", - total points of all subjects for this student of semester 3 } ]</code>
위 내용은 제공된 기사에 맞는 몇 가지 질문 기반 제목은 다음과 같습니다. * LEFT JOIN 및 하위 쿼리를 사용하여 학생 주제를 하나의 레코드로 병합하는 방법은 무엇입니까? * 통합 학생 데이터 구조 만들기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!