+----------+----------+--------+---------------------+--------+---------------------+------+
| id | protype |inputtor| inputtime | confirm| confirmtime | notes|
+----------+----------+--------+---------------------+--------+---------------------+------+
| 100 | 男装 | 1 | 2009-10-28 11:00:00 | 1 | 2009-10-28 12:00:00 | NULL |
| 101 | 衬衫 | 2 | 2009-10-28 11:00:00 | 1 | 2009-10-28 12:00:00 | NULL |
| 102 | 牛仔裤 | 1 | 2009-10-28 11:00:00 | 2 | 2009-10-28 12:00:00 | NULL |
| 200 | 女装 | 1 | 2009-10-28 11:00:00 | 2 | 2009-10-28 12:00:00 | NULL |
| 300 | 男性配饰 | 2 | 2009-10-28 11:00:00 | 2 | 2009-10-28 12:00:00 | NULL |
| 400 | 女性配饰 | 2 | 2009-10-28 11:00:00 | 1 | 2009-10-28 12:00:00 | NULL |
+----------+----------+--------+---------------------+--------+---------------------+------+
表b:employee
+----------+----------+
| id | name |
+----------+----------+
| 1 | 张三 |
| 2 | 李四 |
| 3 | 王五 |
+----------+----------+
输出结果:
+----------+----------+--------+---------------------+--------+---------------------+------+
| id | protype |inputtor| inputtime | confirm| confirmtime | notes|
+----------+----------+--------+---------------------+--------+---------------------+------+
| 100 | 男装 | 张三 | 2009-10-28 11:00:00 | 张三 | 2009-10-28 12:00:00 | NULL |
| 101 | 衬衫 | 李四 | 2009-10-28 11:00:00 | 张三 | 2009-10-28 12:00:00 | NULL |
| 102 | 牛仔裤 | 张三 | 2009-10-28 11:00:00 | 李四 | 2009-10-28 12:00:00 | NULL |
| 200 | 女装 | 张三 | 2009-10-28 11:00:00 | 李四 | 2009-10-28 12:00:00 | NULL |
| 300 | 男性配饰 | 张三 | 2009-10-28 11:00:00 | 李四 | 2009-10-28 12:00:00 | NULL |
| 400 | 女性配饰 | 李四 | 2009-10-28 11:00:00 | 张三 | 2009-10-28 12:00:00 | NULL |
+----------+----------+--------+---------------------+--------+---------------------+------+
我知道的有两个种方法可以实现。
方法一:
SELECT * FROM
(SELECT p.id,p.protype,e.name,p.inputtime FROM product_type AS p JOIN employee AS e ON p.inputtor=e.id) AS a
JOIN
(SELECT p.id,e.name,p.confirmtime,p.notes FROM product_type AS p JOIN employee AS e ON p.confirm=e.id) AS b
ON a.id=b.id;
方法二:
SELECT
id,protype,
(SELECT name FROM employee WHERE employee.id=product_type.inputtor),
inputtime,
(SELECT name FROM employee WHERE employee.id=product_type.confirm),
confirmtime,notes
FROM product_type;
两种方法测试后发现效率相差不大,为什么?
有没有更好的办法?
6 个解决方案
#1
试试这个
你的两个查询。
第一个产生了两个临时记录集,无索引可以再利用了。
第二个,则是每条记录会查二次 employee
select *
from product_type a ,employee i,employee c
where a.inputtor=i.id and a.confirm=c.id
你的两个查询。
第一个产生了两个临时记录集,无索引可以再利用了。
第二个,则是每条记录会查二次 employee
#3
直接连接就OK了
select * from product_type a
inner join
employee b on a.inputtor=b.id
inner join
employee c on a.confirm=c.id
select * from product_type a
inner join
employee b on a.inputtor=b.id
inner join
employee c on a.confirm=c.id
#4
select p.id,p.protype,e1.name as inputtor,p.inputtime,e2.name as confirm,p.confirmtime,p.notes from product_type as p left join employee e1 on p.inputtor=e1.id left join employee e2 on p.confirm=e2.id
#5
受用了,谢谢!
#6
学习了,谢谢!
#1
试试这个
你的两个查询。
第一个产生了两个临时记录集,无索引可以再利用了。
第二个,则是每条记录会查二次 employee
select *
from product_type a ,employee i,employee c
where a.inputtor=i.id and a.confirm=c.id
你的两个查询。
第一个产生了两个临时记录集,无索引可以再利用了。
第二个,则是每条记录会查二次 employee
#2
#3
直接连接就OK了
select * from product_type a
inner join
employee b on a.inputtor=b.id
inner join
employee c on a.confirm=c.id
select * from product_type a
inner join
employee b on a.inputtor=b.id
inner join
employee c on a.confirm=c.id
#4
select p.id,p.protype,e1.name as inputtor,p.inputtime,e2.name as confirm,p.confirmtime,p.notes from product_type as p left join employee e1 on p.inputtor=e1.id left join employee e2 on p.confirm=e2.id
#5
受用了,谢谢!
#6
学习了,谢谢!