如何为我的数据库连接表

时间:2015-04-09 16:38:26

标签: mysql sql join phpmyadmin

我有一个数据库,里面有两个我想加入的表。

Table1: Employee
Table2: Video_Store

这两个表都有一个视频商店ID(VS_ID)。我希望它显示员工的名字和姓氏以及他们与之关联的VS_ID。 当我运行这个陈述时:

select Video_Store.VS_ID, Video_Store.VS_Name, Employee.E_LName, Employee.E_FName
from Video_Store, Employee

这是结果。每个员工只有一个VS_ID而不是六个。如何减少冗余?

1. 1    West Street Borg    James
2. 4    East Street Borg    James
3. 5    North Tremo Borg    James
4. 6    South Stree Borg    James
5. 7    Elm Street  Borg    James
6. 8    Boston Stre Borg    James
7. 1    West Street Wong    Franklin
8. 4    East Street Wong    Franklin
9. 5    North Tremo Wong    Franklin
10. 6   South Stree Wong    Franklin
11. 7   Elm Street  Wong    Franklin
12. 8   Boston Stre Wong    Franklin
13. 1   West Street Freed   Alex
14. 4   East Street Freed   Alex
15 5    North Tremo Freed   Alex
16. 6   South Stree Freed   Alex
17. 7   Elm Street  Freed   Alex
18. 8   Boston Stre Freed   Alex
19. 1   West Street James   Jared
20. 4   East Street James   Jared
21. 5   North Tremo James   Jared
22. 6   South Stree James   Jared
23. 7   Elm Street  James   Jared
24. 8   Boston Stre James   Jared
25. 1   West Street Wallace Jennifer
26. 4   East Street Wallace Jennifer
27. 5   North Tremo Wallace Jennifer
28. 6   South Stree Wallace Jennifer
29. 7   Elm Street  Wallace Jennifer
30. 8   Boston Stre Wallace Jennifer

2 个答案:

答案 0 :(得分:1)

  

这两个表都有一个视频商店ID(VS_ID)。我想要它显示   员工的名字和姓氏以及他们的VS_ID   与。相关联。

确切地说,您只是缺少指定VS_ID条件。

SELECTVideo_Store.VS_ID, Video_Store.VS_Name, Employee.E_LName, Employee.E_FName
FROM Video_Store 
JOIN Employee ON Video_Store.VS_ID = Employee.VS_ID

由于您未指定任何条件,因此您的查询返回了两个表的笛卡尔积。

答案 1 :(得分:0)

使用JOIN ... ON

SELECT v.VS_ID, v.VS_Name, e.E_LName, e.E_FName
FROM Video_Store v
JOIN Employee e
ON v.VS_ID = e.VS_ID