多表查询
我有四个表:
- 角色
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用连接来代替。当您在 FROM 部分中使用逗号分隔表时,您将在结果中获得所有可能的组合。
尝试这个查询(使用联接):
有两件事:
您应该在此处使用别名。 (上面的c、a、ts代替完整的表名称为别名)
如果要与字符'?'精确匹配,请使用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):
Two things:
You should use aliases here. (The c, a, ts above instead of full table names are called aliases)
If you want to match exactly with character '?', use c.name = '?'. c.name like '%?%' will return all c.names which have a ? in them.
看来您缺少 JOIN。使用表别名和格式化更容易阅读:
Looks like you're missing a JOIN. It's easier to read with table aliases and formatted:
您缺少一些表连接。
You were missing some table joins.