ホームページ  >  記事  >  データベース  >  提供された記事に適合する質問ベースのタイトルをいくつか示します。 * LEFT JOIN とサブクエリを使用して学生の科目を 1 つのレコードに結合するにはどうすればよいですか? * 統合された学生データ構造を作成する

提供された記事に適合する質問ベースのタイトルをいくつか示します。 * LEFT JOIN とサブクエリを使用して学生の科目を 1 つのレコードに結合するにはどうすればよいですか? * 統合された学生データ構造を作成する

Susan Sarandon
Susan Sarandonオリジナル
2024-10-26 23:01:31644ブラウズ

Here are a few question-based titles that fit your provided article:

* How to Merge Student Subjects into One Record with a LEFT JOIN and Subqueries?
* Creating a Unified Student Data Structure with Subjects and Semester Scores: A SQL Query Approach
* Ef

1 つの回答で複数の回答データを返す

前の質問では、各学生が 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- 最初の行には生徒の静的データが含まれます。

  • userid: テーブル users のユーザーの ID
  • username: テーブル内のユーザーの名前 users
  • subjectsid: 最初のサブジェクトの IDこの学生の
  • subjectname: 現在この行の現在の科目の名前
  • activepts: 現在の科目のポイント
  • totalpts: この学生の合計ポイント全科目
  • sem1、sem2、sem3: 各学期のこの生徒の合計点

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 サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。