MySQL Join详解
2005-11-06 17:40:07 来源:WEB开发网核心提示: ++---+-+ | id | name | dept_id | ++---+-+ | 6 | Dennis-6 | NULL | | 7 | Dennis-7 | NULL | | 8 | Dennis-8 | NULL | | 9 | Dennis-9 | NULL |
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 把 table emp_dept 放在左边的情形(当然以 emp_dept 中的数据为基础来显示资料,emp 中比emp_dept 中多的资料也就不会显示出来了):
select a.id,a.name,b.dept_id
from emp_dept b left join emp a on (a.id=b.emp_id);
MySQL(和PHP搭配之最佳组合)> select a.id,a.name,b.dept_id
-> from emp_dept b left join emp a on (a.id=b.emp_id);
+------+----------+---------+
| id | name | dept_id |
+------+----------+---------+
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+------+----------+---------+
>> right join
---------------
select a.id,a.name,b.dept_id
from emp a right join emp_dept b on (a.id=b.emp_id);
# 挑资料时以右边 table emp_dept 中的资料为基础来显示资料
MySQL(和PHP搭配之最佳组合)> select a.id,a.name,b.dept_id
-> from emp a right join emp_dept b on (a.id=b.emp_id);
更多精彩
赞助商链接