如何从MySql中的3个不同表中获取数据

时间:2014-02-21 10:35:49

标签: mysql sql

我有一种情况,我必须从三个不同的表中获取数据,我想使用单一查询。我们可以这样做。请帮助我。

在这种情况下:

 table_one

emp_id   dept_id      name
GT102    206          Test
GT103    201          Test1
GT105    111          Test2
GT106    340          Test3

table_two

Dept_id      department

111          Finance
340          Product
201          IT

table_three

emp_id      Salary

GT102       10,000
GT103       20,000
GT105       40,000
GT106       50,000

我想获取Test2的部门和工资。请帮助我

3 个答案:

答案 0 :(得分:0)

试试这个:

Select table_two.department, table_three.Salary From table_one, table_two, table_three where table_one.emp_id = table_three.emp_id and table_one.dept_id = table_two.Dept_id and table_one.name='Test2'

答案 1 :(得分:0)

试试这个:

SELECT t1.name, 
       t2.department, 
       t3.salary 
FROM   table_one t1
       INNER JOIN table_two t2
              ON t2.dept_id = t1.dept_id 
       INNER JOIN table_three t3
              ON t1.emp_id = t3.emp_id 
WHERE  t1.name = 'Test2' 

答案 2 :(得分:0)

select table_two.department,table_three.Salary from table_one join table_two on table_one.dept_id=table_two.dept_id join table_three on table_three.emp_id= table_one.emp_id WHERE table_one.name like 'Test2';