在另一个查询中查询以连接两个表

时间:2012-07-11 10:24:27

标签: mysql sql

我有两张桌子 mstEmp 每日出勤。现在我想运行一个查询

SELECT 
  mstEmp.empname, 
  dailyattendance.InTime, 
  dailyattendance.OutTime, 
  mstEmp.teamtype 
FROM 
  dailyattendance ,
  mstEmp mstEmp 
where 
  dailyattendance.HolderName IN (
    select mstEmp.empname from mstEmp where mstEmp.teamtype='$chk' )

此处 teamtype 匹配,相应的名称从 mstEmp 获取,然后与 dailyattendance.HolderName 匹配以显示结果。

1 个答案:

答案 0 :(得分:0)

这个怎么样::

 SELECT 
    mstEmp.empname, 
    dailyattendance.InTime, 
    dailyattendance.OutTime, 
    mstEmp.teamtype 

    FROM dailyattendance inner join mstEmp  on (dailyattendance.HolderName = mstEmp.empname)

    where mstEmp.teamtype='$chk'
   group by mstEmp.empname