MySql内部连接2表与WHERE子句

时间:2014-07-22 17:58:19

标签: c# mysql .net

我正在查询3个Mysql表:

 Sql = "SELECT " +
       "  u.iUserID, " +
       "  u.szPlayerName, " +
       "  u.szPlayerSurname, " +
       "  u.iPreferredPos, " +
       "  u.iAltPos, " +
       "  u.charType, " +
       "  c.szClubPrefix, "+
       "  tblteam.szTeamName " +

       "FROM " +
       "  tblteam" +
       "  INNER JOIN tblusers u ON (tblteam.iTeamID = u.iTeamID) " +
       "  INNER JOIN tblclubs c ON (u.iClubID = c.iClubID) " +
       "WHERE " +
           "c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 '  AND " +
           "u.bActive = 1 " +
           "AND u.charType= 'c'" +
       "ORDER BY " +
       "  tblteam.szTeamName DESC, " +
       "  u.szPlayerSurname;";

我的问题是;此查询仅返回与c.szClubPrefix = 'CLM'匹配的数据,而不是与u.iClubID2 =' 2 '匹配的数据,请任何人都可以帮助我在这里做错了什么?

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

括在括号中 (c.szClubPrefix =' CLM'或者u.iClubID2 =' 2')

并删除2之前和之后的空格。 请注意,处理MySQL中的数字您不需要'

e.g。 iClubID2 = 2

而不是

iClubID2 =' 2'

答案 1 :(得分:1)

根据您的偏好添加括号

"(c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ')  AND " +
           "u.bActive = 1 " +
           "AND u.charType= 'c'" 

答案 2 :(得分:1)

你的where子句如下所示:

在哪里c.szClubPrefix =' CLM'或者u.iClubID2 =' 2' AND u.bActive = 1 AND u.charType =' c'

它等同于:

在哪里c.szClubPrefix =' CLM'或(u.iClubID2 =' 2' AND u.bActive = 1 AND u.charType =' c')

所以,u.iClubID2 =' 2'是这三个条件中的一个必须都是真的。