1

我有四张桌子:

  • 人物
  • arena_team
  • arena_table_member
  • arena_team_stats。

字符表有 guid,名称

arena_team 表有 arenateamid, name, type

arena_table_member 表有 guid(这与字符表中的相同), arenateamid

arena_team_stats 表有 arenateamid、rating、wins、wins2、played

如何获取角色所在的竞技场团队列表?我试过:

$result=mysql_query("SELECT 
          characters.guid
           , characters.name
           , arena_team.arenateamid
           , arena_team.name
           , arena_team_stats.rating
           , arena_team_stats.wins
           , arena_team_stats.wins2
           , arena_team_stats.played
           , arena_team.type 
   FROM characters
           , arena_team_stats
           , arena_team 
    WHERE characters.name LIKE '%$q%' 
    AND arena_team.arenateamid = arena_team_stats.arenateamid 
    ORDER BY arena_team_stats.rating DESC") 
or die(mysql_error());

但它会返回 arena_team 表中的所有竞技场团队。

4

3 回答 3

3

您应该改用联接。当您在 FROM 部分中使用逗号分隔表时,您会在结果中获得所有可能的组合。

试试这个查询(使用联接):

SELECT c.guid,c.name,a.arenateamid,a.name,
  ts.rating,ts.wins,ts.wins2,
  ts.played,a.type 
 FROM characters c
         JOIN arena_table_member ON arena_table_member.guid=character.guid
  JOIN arena_team a ON arena_table_member.arenateamid = a.arenateamid
  JOIN arena_team_stats ts ON ts.arenateamid = a.arenateamid
 WHERE c.name LIKE '%$q%' 
  AND a.arenateamid = ts.arenateamid 
 ORDER BY ts.rating DESC

两件事情:

  1. 您应该在这里使用别名。(上面的 c、a、ts 不是完整的表名被称为别名)

  2. 如果要与字符“?”完全匹配,请使用 c.name = '?'。c.name like '%?%' 将返回所有具有 ? 在他们中。

于 2010-07-25T17:13:34.463 回答
1

看起来你缺少一个 JOIN。使用表别名和格式化更容易阅读:

    SELECT c.guid
         , c.name
         , at.arenateamid
         , at.name
         , at.type
         , ats.rating
         , ats.wins
         , ats.wins2 
         , ats.played
      FROM characters c
INNER JOIN arena_team_member atm ON atm.guid = c.guid
INNER JOIN arena_team at ON at.arenateamid = atm.arenateamid
INNER JOIN arena_team_stats ats ON ats.arenateamid = at.arenateamid
     WHERE c.name LIKE '%$q%'
  ORDER BY ats.rating DESC
于 2010-07-25T17:17:52.113 回答
0
SELECT Char.guid, Chars.name, Teams.arenateamid, Teams.name, Stats.rating,
       Stats.wins, Stats.wins2, Stats.played, Teams.type
FROM characters AS Chars,arena_team_stats AS Stats, arena_team AS Teams,
     arena_table_member AS Members
WHERE Chars.name LIKE '%$q%' AND Teams.arenateamid = Stats.arenateamid AND
      Members.guid = Chars.guid AND Members.arenateamid = Teams.arenateamid
ORDER BY Stats.rating DESC

您缺少一些表连接。

于 2010-07-25T17:20:52.280 回答