Sql查询结合两个表的结果

时间:2013-10-01 05:16:10

标签: mysql sql

目前我使用以下查询显示以下结果。

SELECT * FROM RouteToGrowthRecord, GradeMaster,MileStoneMaster
WHERE MemberID = 'ALV01L11034A06' AND
RouteToGrowthRecord.GradeID=GradeMaster.GradeID AND
RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID 
ORDER BY CheckupDate DESC

and the output of quiry is:

现在我有另一个名为RouteToGrowthRecord_st的表具有相同的表 列为RouteToGrowthRecord,包含一些其他字段。

我需要显示表中存在的结果。即。如果RouteToGrowthRecord_st有3条带有给定menberID的记录,则输出必须包含3条记录以及上述查询结果。(fr ex高于其9 + 3 =总共12条记录)。

4 个答案:

答案 0 :(得分:0)

    SELECT * FROM RouteToGrowthRecord a inner join GradeMaster b inner 
  join MileStoneMaster c inner join  RouteToGrowthRecord_st d on 
    a.GradeID=b.GradeID AND   a.MileStoneID=c.MileStoneID   and
    d.GradeID=b.GradeID AND  d.MileStoneID=c.MileStoneID 
    WHERE a.MemberID = 'ALV01L11034A06' 
    ORDER BY CheckupDate DESC

答案 1 :(得分:0)

您可以按以下方式编写上述查询

SELECT * FROM RouteToGrowthRecord 
INNER JOIN GradeMaster ON  RouteToGrowthRecord.GradeID=GradeMaster.GradeID
INNER JOIN MileStoneMaster ON RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
LEFT JOIN RouteToGrowthRecord_st ON    RouteToGrowthRecord_st.memberID=RouteToGrowthRecord.memberID 
WHERE
RouteToGrowthRecord.MemberID = 'ALV01L11034A06'
order by CheckupDate DESC

答案 2 :(得分:0)

您可以在此处使用Union来合并从两个查询获得的结果。使用未映射的其他字段的默认值。

答案 3 :(得分:0)

  

这是我的回答

SELECT CheckUpDate,AgeInMonths,PresentWeight,Height,Diagnosis,growthstatus,GradeName,MilestoneName,MemberID
FROM RouteToGrowthRecord, GradeMaster,MileStoneMaster WHERE
 MemberID = 'ALV01L56107A11  ' and 
  RouteToGrowthRecord.GradeID=GradeMaster.GradeID and 
  RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID 
  union
  SELECT CheckUpDate,AgeInMonths,PresentWeight,Height,Diagnosis,growthstatus,GradeName,MilestoneName,MemberID
   FROM RouteToGrowthRecord_st, GradeMaster,MileStoneMaster WHERE
 MemberID = 'ALV01L56107A11  ' and 
  RouteToGrowthRecord_st.GradeID=GradeMaster.GradeID and 
  RouteToGrowthRecord_st.MileStoneID=MileStoneMaster.MileStoneID 
  order by CheckupDate DESC