查询别名问题

时间:2018-08-13 05:03:41

标签: mysql

SELECT 
`tblTrainingSessionProgramming`.`trainingSessionProgramID`, `tblExercises`.`exerciseName`, `tblRepSchemes`.`repScheme`
FROM 
`tblExercises` , `tblRepSchemes`
LEFT JOIN 
`tblTrainingSessionProgramming` ON `tblExercises`.`exerciseID` = `tblTrainingSessionProgramming`.`exerciseID` 
LEFT JOIN 
`tblTrainingSessionProgramming` ON `tblRepSchemes`.`repSchemeID` = `tblTrainingSessionProgramming`.`repSchemeID`

我正在尝试运行此查询,但出现错误

  

1066-不是唯一的表/别名:“ tblTrainingSessionProgramming”

当我尝试对该表进行别名时,它说它找不到另一个数据库列。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您可以尝试一下:

string strSql = "SELECT DISTINCT TableID AS [Key],TableName AS [Value] FROM dbo.TS_TStuctMaster";
Dictionary<string,string> dicts = sqlConnection.Query<KeyValuePair<string,string>>(strSql).ToDictionary(pair => pair.Key, pair => pair.Value);

答案 1 :(得分:1)

您要两次连接同一张表,请为每个联接指定唯一的别名,并在引用列时使用这些别名。您还需要加入表tblRepSchemes

或者您可以改用FROM tblTrainingSessionProgramming并以其他方式加入

SELECT `tblTrainingSessionProgramming`.`trainingSessionProgramID`, `tblExercises`.`exerciseName`, `tblRepSchemes`.`repScheme`
FROM tblTrainingSessionProgramming 
JOIN `tblExercises ON `tblExercises`.`exerciseID` = `tblTrainingSessionProgramming`.`exerciseID` 
JOIN `tblRepSchemes` ON `tblRepSchemes`.`repSchemeID` = `tblTrainingSessionProgramming`.`repSchemeID`