带WHERE子句的2个表的内部连接

本文关键字:内部 连接 2个 子句 WHERE | 更新日期: 2023-09-27 18:14:06

我正在查询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 '匹配的数据,请任何人帮助我,我在这里做错了什么?

谢谢你的帮助。

带WHERE子句的2个表的内部连接

根据您的偏好添加括号

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

用圆括号括起(c.szClubPrefix =' CLM' OR u.iClubID2 ='2')

,并删除2前后的空白。注意,在MySQL中处理数字不需要'

。iClubID2 = 2

不是

iClubID2 = ' 2 '

where子句如下:

WHERE c.s szclubprefix =' CLM' OR u.c iclubid2 =' 2 ' AND u.c bactive = 1 AND u.c chartype =' c'

与it等价:

WHERE c.s szclubprefix =' CLM' OR (u.c iclubid2 =' 2 ' AND u.bActive = 1 AND u.c chartype =' c')

因此,u.i iclubid2 =' 2 '是这三个条件之一,必须全部为真