如何使用JOIN Query获取相关表数据

时间:2017-03-20 07:59:44

标签: php sql join

course{
    course_id,
    course_name,
}
subject{
    subject_id,
    subject_name,
    course_id

}
student{
    email,
    course_id,
}

这是我的代码,它显示属于课程的所有科目

$_GET['email_address']=$_SESSION['email_address'];


$sql="(SELECT c.course_name course_name,su.subject_name subject_name 
   from subject su LEFT JOIN course c ON c.course_id=su.course_id) 
   UNION (SELECT c.course_name course_name,s.email_address email_address 
     from student s LEFT JOIN course c ON s.course_id=c.course_id
    WHERE s.email_address='".$_SESSION['email_address']."')";

当课程由" kamal@gmail.com"完成时,我希望所有科目都属于" kamal@gmail.com"所做的课程, kamal@gmail.com是学生邮件

样本数据是:

当然:

course_id course_name
1         IT
2         Business
3         Design

主题:

subject_id subject_name course_id
111        html         1
222        java         1
333        Econ         2
444        Photoshop    3 

学生:

email           course_id
kamal@gmail.com 1

然后我想通过kamal@gmail.com获取哪些主题属于course_id = 1的数据

3 个答案:

答案 0 :(得分:0)

您需要的几个连接

// Handler for moving the circle and moving the inscription inside
procedure dyn_frm.SelectionPoint1Track(Sender: TObject;
  var X, Y: Single);
begin
 LblPhoto[(Sender as TSelectionPoint).Tag].Position.X:=X-10;
 LblPhoto[(Sender as TSelectionPoint).Tag].Position.Y:=Y-10;
end;

答案 1 :(得分:0)

以下情况如何?

<VirtualHost *:7777>
        ServerAdmin webmaster@localhost
        ServerName myexample.com/
        DocumentRoot /var/www/web/
        Redirect permanent / https://myexample.com/
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerName myexample.com/
        DocumentRoot /var/www/web/
        Header always append X-Frame-Options SAMEORIGIN
        Header always set X-XSS-Protection "1; mode=block"
        Header always unset Server
        Header always set X-Content-Type-Options nosniff
        <Directory />
                Options -Indexes -FollowSymLinks -MultiViews
                AllowOverride None
        </Directory>
        <Directory /var/www/web/>
                LimitRequestBody 512000
                Options -Indexes -FollowSymLinks -MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -Indexes -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
         SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        TraceEnable off

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

答案 2 :(得分:0)

  

以下是有关您的问题的数据库设计的建议

表: courses 有很多subjects

+-----------+-------------+---------------------+
| course_id | course_name | created             |
+-----------+-------------+---------------------+
|         1 | CSS         | 2017-03-20 14:21:34 |
|         2 | HTML        | 2017-03-20 14:21:39 |
|         3 | JS          | 2017-03-20 14:21:44 |
|         4 | PHP         | 2017-03-20 14:21:50 |
+-----------+-------------+---------------------+

表: students 有很多courses

+------------+--------------+------------------+---------------------+
| student_id | student_name | email            | created             |
+------------+--------------+------------------+---------------------+
|          1 | Student 1    | kamal@gmail.com  | 2017-03-20 14:19:32 |
|          2 | Student 2    | kamal2@gmail.com | 2017-03-20 14:19:32 |
+------------+--------------+------------------+---------------------+

表格 subjects 属于courses

+------------+-----------+--------------+---------------------+
| subject_id | course_id | subject_name | created             |
+------------+-----------+--------------+---------------------+
|          1 |         1 | A            | 2017-03-20 14:20:56 |
|          2 |         1 | B            | 2017-03-20 14:21:12 |
|          3 |         2 | C            | 2017-03-20 14:21:22 |
+------------+-----------+--------------+---------------------+

表: courses_students 多对多courses students

+----+-----------+------------+---------------------+
| id | course_id | student_id | created             |
+----+-----------+------------+---------------------+
|  1 |         1 |          1 | 2017-03-20 14:44:42 |
|  2 |         2 |          2 | 2017-03-20 14:44:42 |
|  3 |         2 |          1 | 2017-03-20 14:44:42 |
+----+-----------+------------+---------------------+
  

注意: courses有很多学生,students有很多courses。这是映射表。

MySQL查询:

SELECT
  students.student_name,
  courses.course_name,
  subjects.subject_name
FROM
  `students`
LEFT JOIN
  courses_students ON students.student_id = courses_students.student_id
LEFT JOIN
  subjects ON subjects.course_id = courses_students.course_id
INNER JOIN
  courses ON courses.course_id = courses_students.course_id
WHERE
  students.email = 'kamal@gmail.com'

MySQL查询结果:

+--------------+-------------+--------------+
| student_name | course_name | subject_name |
+--------------+-------------+--------------+
| Student 1    | CSS         | A            |
| Student 1    | CSS         | B            |
| Student 1    | HTML        | C            |
+--------------+-------------+--------------+