多表查询

发布于 2024-09-11 20:13:48 字数 949 浏览 4 评论 0原文

我有四个表:

  • 角色
  • arena_team
  • arena_table_member
  • arena_team_stats。

角色表有 guid,名称

arena_team 表有 arenateamid,名称,类型

arena_table_member 表有 guid(这与角色表中的相同),arenamid

arena_team_stats 表有 arenateamid,评级,胜利,wins2,玩过

我如何获取竞技场列表团队的性格在哪里?我尝试过:

$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 表中的所有竞技场球队。

I have four tables:

  • characters
  • arena_team
  • arena_table_member
  • arena_team_stats.

characters table has guid, name

arena_team table has arenateamid, name, type

arena_table_member table has guid(this is the same as in characters table), arenateamid

arena_team_stats table has arenateamid, rating, wins, wins2, played

How do I get the list of arena teams where character is? I tried:

$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());

but it returns all arena teams which are in arena_team table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-09-18 20:13:48

您应该使用连接来代替。当您在 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 '%?%' 将返回所有带有 ? 的 c.name在他们之中。

You should use Joins instead. When you use comma separated table in the FROM part, you get all possible combinations in the result.

Try this query (using Joins):

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

Two things:

  1. You should use aliases here. (The c, a, ts above instead of full table names are called aliases)

  2. If you want to match exactly with character '?', use c.name = '?'. c.name like '%?%' will return all c.names which have a ? in them.

雨落星ぅ辰 2024-09-18 20:13:48

看来您缺少 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

Looks like you're missing a JOIN. It's easier to read with table aliases and formatted:

    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
夕色琉璃 2024-09-18 20:13:48
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

您缺少一些表连接。

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

You were missing some table joins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文