需要帮助限制 Transact-sql 中的联接
我对 SQL 有点陌生,需要查询语法方面的帮助。
我的问题涉及 Transact-SQL(MS SQL Server 2000 查询分析器)下较大的多表联接中的 2 个表,
我有 ACCOUNTS 和 LOGINS,它们在 2 个字段上联接:站点和帐户。子集。 对于每个站点/子集组合,两个表都可以有多个行。
帐户: |登录: 站点子集 字段 字段 |站点子集 用户 ID 密码 阿尔法布拉沃等等等等|阿尔法·布拉沃·富酒吧 阿尔法查理等等等等 |阿尔法布拉沃酒吧富 阿尔法查理布莱布莱布莱|阿尔法查理布莱布莱布莱|阿尔法查理的自我 三角洲布拉沃等等等等|三角洲布拉沃约翰欢迎 三角洲狐步舞等等等等|三角洲布拉沃简欢迎 |三角洲布拉沃肯欢迎 | delta bravo barbara 欢迎
我想选择帐户中具有登录条目的所有行,但每个帐户仅 1 登录。
所需结果: 站点子集字段字段字段用户 ID 密码 alpha bravo 等等等等 foo 酒吧 阿尔法查理等等等等我的自我 阿尔法查理布莱布莱布莱蓝色身份自我 delta bravo blah blah blah jane 欢迎
我并不关心登录表中的哪一行,但用户 ID 和密码必须对应。 [不要返回无效的组合,如 foo
/foo
或 bar
/bar
] MS Access 有一个方便的 FIRST函数,它可以做到这一点,但我还没有在 TSQL 中找到等效的函数。
另外,如果有区别的话,其他表也会连接到 ACCOUNTS,但这是结构中 LOGINS 的唯一用途。
非常感谢您非常的帮助。
快速添加信息:USERID/PASSWD 组合在整个 LOGIN 表中是唯一的。
I'm somewhat new to SQL and need help with query syntax.
My issue involves 2 tables within a larger multi-table join under Transact-SQL (MS SQL Server 2000 Query Analyzer)
I have ACCOUNTS and LOGINS, which are joined on 2 fields: Site & Subset.
Both tables may have multiple rows for each Site/Subset combination.
ACCOUNTS: | LOGINS: SITE SUBSET FIELD FIELD FIELD | SITE SUBSET USERID PASSWD alpha bravo blah blah blah | alpha bravo foo bar alpha charlie blah blah blah | alpha bravo bar foo alpha charlie bleh bleh blue | alpha charlie id ego delta bravo blah blah blah | delta bravo john welcome delta foxtrot blah blah blah | delta bravo jane welcome | delta bravo ken welcome | delta bravo barbara welcome
I want to select all rows in ACCOUNTS which have LOGIN entries, but only 1 login per account.
DESIRED RESULT: SITE SUBSET FIELD FIELD FIELD USERID PASSWD alpha bravo blah blah blah foo bar alpha charlie blah blah blah id ego alpha charlie bleh bleh blue id ego delta bravo blah blah blah jane welcome
I don't really care which row from the login table I get, but the UserID and Password have to correspond. [Don't return invalid combinations like foo
/foo
or bar
/bar
] MS Access has a handy FIRST function, which can do this, but I haven't found an equivalent in TSQL.
Also, if it makes a difference, other tables are joined to ACCOUNTS, but this is the only use of LOGINS in the structure.
Thank you very much for any assistance.
Quick added info: USERID/PASSWD combinations are unique across the entire LOGIN table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我尝试了其中的几个答案,但成功程度有限。
我最终通过连接 USERID 和 USERID 解决了这个问题。 select 语句中的 PASSWD 字段并使用 MAX 值时仅返回 1。
换句话说:
I tried several of these answers, with limited degrees of success.
I eventually solved the issue by concatenating the USERID & PASSWD fields in the select statement and used the MAX value to return only one.
In other words:
使用自动编号字段会更容易。您实际上没有该表的良好主键。
编辑:
如果无法更改架构,则始终可以将记录转储到具有自动编号的临时表中,但如果经常运行,则可能效率低下,但它会起作用。
再次编辑:
如果您采用临时表方法,则代码为:
It would be easier with an autonumber field. You don't really have a good primary key for this table.
Edit:
If you can't change schema, you can always dump the records into a temp table with an autonumber, but it may be inefficient if this is being run often, but it will work.
Edit again:
If you went the temp table approach, the code for that is:
这是一个可能有效的查询,
内部 SELECT 只为每个帐户提供一次登录,然后将其连接回帐户以获得最终结果。
Here is a query that may work
The Inner SELECT gets you only one Login per Account and then you join this back to the Accounts to get your final result.
假设 USERID/PASSWD 组合对于每个站点/子集组合都是唯一的,这可能会起作用,
也可以尝试这个来看看它是否运行得更快
(实际上并没有尝试运行这些,因此语法错误可能已经出现在某个地方)
This may work assuming that USERID/PASSWD combos are unique per SITE/SUBSET combo
also try this to see if it runs faster
(didn't actually try running these, so a syntax error may have crept in somewhere)