在MSSQL中连接三个表
我在 MSSQL 中有一个游戏的三个表,其结构如下:
CHARS 这是它的查找表:
- strAccount 链接到 NGSCUSER 的主键,它是 帐户用户名
- strChar01,这是第一个字符槽
- strChar02,这是第二个字符槽
- strChar03,这是第三个字符槽
这些具有 gameuser 表的主键,它们是角色名称
NGSCUSER
- strUserID 帐户用户名
- strPasswd 帐户密码
GAMEUSER
- strUserId 这是角色name
- sRank 角色是否是游戏高手
我需要做的是将登录详细信息与查询中的 PHP 变量 $username 和 $password 进行比较,最重要的是检查它们关联的字符并找出它的 sRank 是否为 1
我假设我需要某种类型的三表连接,但我不知道如何进行任何示例或帮助将不胜感激
这是我尝试过的查询到目前为止,但它在第三行出错,“CHARS”附近的语法不正确:
SELECT NGSCUSER.strUserID, NGSCUSER.strPasswd, GAMEUSER.sRank
FROM 'CHARS'
INNER JOIN NGSCUSER ON NGSCUSER.strUserId = CHARS.strAccount
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar01
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar02
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar03
WHERE NGSCUSER.strUserId='$username' and NGSCUSER.strPasswd='$password' and GAMEUSER.sRank = '1'
I have three tables in MSSQL for a game which are structured as follows:
CHARS
This is the look up table it has:
- strAccount which is linked to the primary key of NGSCUSER it is the
account username - strChar01 which is the first character slot
- strChar02 which is the second character slot
- strChar03 which is the third character slot
these have the primary key of the gameuser table inside they are character names
NGSCUSER
- strUserID account username
- strPasswd account password
GAMEUSER
- strUserId this is the character name
- sRank whether the character is a game master
What I need to be able to do in is compare the login details against the PHP variables $username and $password in the query and most importantly check their associated character and find out whether it has a sRank of 1
I assume I need a three table join of some kind but I don't know how to go about it any examples or help would be much appreciated
This is the query I've tried to build so far but it errors on the third line with incorrect syntax near 'CHARS':
SELECT NGSCUSER.strUserID, NGSCUSER.strPasswd, GAMEUSER.sRank
FROM 'CHARS'
INNER JOIN NGSCUSER ON NGSCUSER.strUserId = CHARS.strAccount
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar01
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar02
INNER JOIN GAMEUSER ON GAMEUSER.strUserId = CHARS.strChar03
WHERE NGSCUSER.strUserId='$username' and NGSCUSER.strPasswd='$password' and GAMEUSER.sRank = '1'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,要从字面上回答您的问题(这可能无法解决您的问题),您需要为表添加别名以连接多个表:
但是,如果您尝试拉回
gameuser
行,匹配任何字符槽,那么您需要使用in
:而且,既然我引起了您的注意,我想做以下 PSA:
$username 和
$password
这样您不易受到 SQL 注入的影响。select * from ngscuser
并获取每个人的密码,那你就太丢脸了。耻辱。耻辱。耻辱。Firstly, to literally answer your question (which probably doesn't solve your problem), you need to alias your tables to join multiple tables:
However, if you're trying to pull back the
gameuser
row that matches any character slot, then you need to usein
:And, now that I have your attention, I would like to do the following PSAs:
$username
and$password
so that you are not susceptible to SQL injection.select * from ngscuser
and get everybody's password, shame on you. Shame. Shame. Shame.它应该只是 CHARS 而不是 'CHARS'
It should be just CHARS not 'CHARS'
这是一个处理您所要求的事情的示例函数:
Here is a sample function to handle something like you are asking for: