MySQL的sql查询语句问题,有关join

gxgl168 2006-03-30 08:28:03
希望表格显示A表modify_log中的ID、用户名、操作IP、更新日期,B表usermanager中的姓名、单位,C表news中的标题。A表用户名对应B表的帐号,A表的记录ID对应C表的ID。我的SQL如下,但只返回部分记录:

SELECT a.`ID` , a.`用户名` ,`姓名`,`单位`,a.`操作IP`,a.`更新日期`,`操作类别`,`标题` FROM modify_log a INNER JOIN usermanager b ON a.`用户名` = b.`帐户` INNER JOIN news c ON a.`记录ID` = c.`ID` order by a.`ID` desc
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
followyou 2006-03-31
  • 打赏
  • 举报
回复
SELECT a.`ID` , a.`用户名` ,`姓名`,`单位`,a.`操作IP`,a.`更新日期`,`操作类别`,`标题` FROM modify_log a LEFT JOIN usermanager b ON a.`用户名` = b.`帐户` LEFT JOIN news c ON a.`记录ID` = c.`ID` order by a.`ID` desc
littleboys 2006-03-31
  • 打赏
  • 举报
回复
还是先 Create table 吧

create table emp(
id int not null primary key,
name varchar(10)
);

create table emp_dept(
dept_id varchar(4) not null,
emp_id int not null,
emp_name varchar(10),
primary key (dept_id,emp_id));

insert into emp() values
(1,"Dennis-1"),
(2,"Dennis-2"),
(3,"Dennis-3"),
(4,"Dennis-4"),
(5,"Dennis-5"),
(6,"Dennis-6"),
(7,"Dennis-7"),
(8,"Dennis-8"),
(9,"Dennis-9"),
(10,"Dennis-10");

insert into emp_dept() values
("R&D",1,"Dennis-1"),
("DEv",2,"Dennis-2"),
("R&D",3,"Dennis-3"),
("Test",4,"Dennis-4"),
("Test",5,"Dennis-5");
("dddd",20,"eeee");
>> left join
-------------
select a.id,a.name,b.dept_id
from emp a left join emp_dept b on (a.id=b.emp_id);

# 挑出左边的 table emp 中的所有资料,即使 emp_dept 中没有的资料也挑出来,没有的就用 NULL 来显示,
# 也即显示资料是以左边的 table emp 中的资料为基础

mysql> select a.id,a.name,b.dept_id
-> from emp a left join emp_dept b on (a.id=b.emp_id);
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 1 | Dennis-1 | R&D |
| 2 | Dennis-2 | DEv |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 挑出 table emp 中有而 table emp_dept 中没有的人员资料
select a.id,a.name,b.dept_id
from emp a left join emp_dept b on (a.id=b.emp_id)
where b.dept_id IS NULL;

mysql> select a.id,a.name,b.dept_id
-> from emp a left join emp_dept b on (a.id=b.emp_id)
-> where b.dept_id IS 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> 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 |
+------+----------+---------+
| NULL | NULL | dddd |
| 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> select a.id,a.name,b.dept_id
-> from emp a right join emp_dept b on (a.id=b.emp_id);
+------+----------+---------+
| id | name | dept_id |
+------+----------+---------+
| NULL | NULL | dddd |
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+------+----------+---------+
6 rows in set (0.00 sec)
# 我们再把 table 的位置交换一下,再用 right join 试试

select a.id,a.name,b.dept_id
from emp_dept b right join emp a on (a.id=b.emp_id);

mysql> select a.id,a.name,b.dept_id
-> from emp_dept b right join emp a on (a.id=b.emp_id);
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 1 | Dennis-1 | R&D |
| 2 | Dennis-2 | DEv |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 是不是和 left join 一样了?
>> inner join STRAIGHT_JOIN
select a.id,a.name,b.dept_id
from emp a ,emp_dept b
where a.id=b.emp_id;

mysql> select a.id,a.name,b.dept_id
-> from emp a ,emp_dept b
-> where 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 |
+----+----------+---------+
xvhfeng 2006-03-30
  • 打赏
  • 举报
回复
从来不用join
mathematician 2006-03-30
  • 打赏
  • 举报
回复
用左连接
gxgl168 2006-03-30
  • 打赏
  • 举报
回复
对SQL不是太熟,麻烦大侠示范一下,谢谢

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧