使用MySQL加入三个表

时间:2010-09-14 13:58:59

标签: mysql

我有三个名为

的表
**Student Table**
-------------
id    name
-------------
1     ali
2     ahmed
3     john
4     king

**Course Table**
-------------
id    name
-------------
1     physic
2     maths
3     computer
4     chemistry

**Bridge**
-------------
sid    cid
-------------
1     1
1     2
1     3
1     4
2     1
2     2
3     3
3     4
4     1
4     2

现在显示学生姓名和他曾学过的课程名称,

**Result**
---------------------------
Student        Course
---------------------------
ahmed         physic
ahmed         maths
ahmed         computer
ahmed         chemistry
ali           physic
ali           maths
john          computer
john          chemistry
king          physic
king          maths

我构建以下查询

select s.name as Student, c.name as Course from student s, course c join bridge b on c.id = b.cid order by s.name

但它没有返回所需的结果......

如果我想找到经理而不是其他人,那么规范化形式会是什么:

**employee**
-------------------
id        name
-------------------
1         ali
2         king
3         mak
4         sam
5         jon

**manage**
--------------
mid      eid
--------------
1         2
1         3
3         4
4         5

想得到这个结果:

**result**
--------------------
Manager      Staff
--------------------
ali          king
ali          mak
mak          sam
sam          jon

10 个答案:

答案 0 :(得分:174)

使用ANSI语法,您将如何更清楚地加入表格:

SELECT s.name as Student, c.name as Course 
FROM student s
    INNER JOIN bridge b ON s.id = b.sid
    INNER JOIN course c ON b.cid  = c.id 
ORDER BY s.name 

答案 1 :(得分:135)

只需使用:

select s.name "Student", c.name "Course"
from student s, bridge b, course c
where b.sid = s.sid and b.cid = c.cid 

答案 2 :(得分:16)

用于规范化表格

select e1.name as 'Manager', e2.name as 'Staff'
from employee e1 
left join manage m on m.mid = e1.id
left join employee e2 on m.eid = e2.id

答案 3 :(得分:4)

SELECT *
FROM user u
JOIN user_clockits uc ON u.user_id=uc.user_id
JOIN clockits cl ON cl.clockits_id=uc.clockits_id
WHERE user_id = 158

答案 4 :(得分:2)

SELECT 
employees.id, 
CONCAT(employees.f_name," ",employees.l_name) AS   'Full Name', genders.gender_name AS 'Sex', 
depts.dept_name AS 'Team Name', 
pay_grades.pay_grade_name AS 'Band', 
designations.designation_name AS 'Role' 
FROM employees 
LEFT JOIN genders ON employees.gender_id = genders.id 
LEFT JOIN depts ON employees.dept_id = depts.id 
LEFT JOIN pay_grades ON employees.pay_grade_id = pay_grades.id 
LEFT JOIN designations ON employees.designation_id = designations.id 
ORDER BY employees.id;

您可以像上面的示例一样加入多个表格。

答案 5 :(得分:1)

使用此:

SELECT s.name AS Student, c.name AS Course 
FROM student s 
  LEFT JOIN (bridge b CROSS JOIN course c) 
    ON (s.id = b.sid AND b.cid = c.id);

答案 6 :(得分:0)

别那样加入。这是一个非常糟糕的做法!!!这将减慢海量数据获取的性能。例如,如果每个表中有100行,则数据库服务器必须提取100x100x100 = 1000000次。它必须提取1 million次。为了解决该问题,请连接前两个表,以尽可能减少匹配的结果(取决于数据库架构)。在Subquery中使用该结果,然后将其与第三个表连接并获取它。对于第一次加入-> 100x100= 10000次,并假设我们得到5个匹配结果。然后,我们将第三个表与结果-> 5x100 = 500.只提取= 10000+500 = 10200次连接起来。因此,性能提高了!!!

答案 7 :(得分:0)

只需在以前的答案中添加一点,我们就可以在MySQL中使用

table_factor syntax 

OR

joined_table syntax

mysql documentation

Table_factor示例

SELECT prd.name, b.name 
FROM products prd, buyers b

联接表示例

SELECT prd.name, b.name 
FROM products prd
 left join buyers b on b.bid = prd.bid;

仅供参考:请忽略以下事实:联接表示例中的左联接没有多大意义(实际上,我们将使用某种联接表将买方链接到产品表,而不是在产品表中保存BuyerID)。 / p>

答案 8 :(得分:0)

Query for three table join and limit set

SELECT * FROM (SELECT t1.follower_userid, t2.*, t3.login_thumb, t3.login_name, 
  t3.bio, t3.account_status, t3.gender
     FROM videos t2
      LEFT JOIN follower t1
        ON t1.follower_userid = t2.user_id 
         LEFT JOIN videos_user t3 
          ON t1.follower_userid  = t3.login_userid
           WHERE t1.following_userid='$userid'
            LIMIT $startpoint , $limit) AS ID 
             ORDER BY ID DESC

答案 9 :(得分:-1)

加入两个以上表的查询:

SELECT ops.field_id, ops.option_id, ops.label
FROM engine4_user_fields_maps AS map 
JOIN engine4_user_fields_meta AS meta ON map.`child_id` = meta.field_id
JOIN engine4_user_fields_options AS ops ON map.child_id = ops.field_id 
WHERE map.option_id =39 AND meta.type LIKE 'outcomeresult' LIMIT 0 , 30