前の質問では、各学生が 1 つの回答になるように、全学期のすべての科目を 1 つのレコードにマージする方法について質問していました。彼のデータは次の例のようになります:
<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>
それで、私は 1 つだけの答えを得ましたが、その答えはその方法を教えてくれませんでした。LEFT JOIN を使用する必要がある理由を教えてくれただけです。そして、私の場合に LEFT JOIN が何をするのかを説明してくれるので、何をする必要があるのかをもう一度説明します。その後、コードといくつかの例でこれを行う方法を説明します。
1- 各生徒は 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 ステートメントでは、ご覧のとおり 2 つのグループに分かれています。一番上の最初のフィールドは学生用の静的フィールドで、残りはテーブル tsubjects
2 から値を取得します。LEFT JOIN 関数は、値を取得する役割を果たします。テーブル tsubjects からすべて (userid) を取得し、userid 間の共通キーによってそれらをマージします。つまり、各ユーザーは、最初のサブジェクト (ORDER BY id ASC) を含む少なくとも 1 つの行で表され、次に他のすべてのサブジェクトが含まれることになります。すでに 1 つの行にマージされているそれらは、学生の静的フィールドとして残りの行で繰り返されます
<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 とサブクエリを使用して学生の科目を 1 つのレコードに結合するにはどうすればよいですか? * 統合された学生データ構造を作成するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。