简体   繁体   English

SQL:别名列上的内连接

[英]SQL: inner join on alias column

Previously I have asked to strip text from a field and convert it to an int, this works successfully. 以前我曾要求从字段中删除文本并将其转换为int,这可以成功运行。 But now, I would like to do an INNER JOIN on this new value. 但是现在,我想对这个新值进行INNER JOIN。

So I have this: 所以我有这个:

SELECT CONVERT(int, SUBSTRING(accountingTab.id, PATINDEX('%[0-9]%', accountingTab.id), 999)) 
AS 'memId',  userDetails.title, userDetails.lname 
FROM accountingTab INNER JOIN
(SELECT id, title, first, last FROM memDetTab) AS userDetails ON memID = userDetails.id

And then I get the Invalid Column Name memID error. 然后我得到无效列名称memID错误。

How can I fix this? 我怎样才能解决这个问题?

You can either repeat the whole expression or reverse your join: 您可以重复整个表达式或反转您的联接:


SELECT *
FROM memDetTab
    JOIN (SELECT CONVERT(int, SUBSTRING(accountingTab.id, PATINDEX('%[0-9]%', accountingTab.id), 999)) AS 'memId', userDetails.title, userDetails.lname
FROM accountingTab) subquery
    ON subquery.memID = memDetTab.ID

而不是memId,重复整个表达式。

If you have to do this, you have design problems. 如果必须这样做,则存在设计问题。 If you're able, I would suggest you need to refactor your table or relationships. 如果你能,我建议你需要重构你的桌子或关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM