SQL - 被 SELECT 困扰 - 请帮忙!
我试图在 SQL Server 2005 中实现以下目标:
SELECT (IF EITHER EXISTS) usr.username, pro.email FROM table1 AS usr, table2 AS pro WHERE usr.username = 'existing_username' AND / OR pro.email = 'existing_email'
我不知道如何编写类似的内容。基本上,我希望它在找到现有用户名时返回用户名,如果找到则返回电子邮件。
所以它会返回给我:用户名,电子邮件,两者或无
这可能吗???
I'm trying to achieve the following in SQL Server 2005:
SELECT (IF EITHER EXISTS) usr.username, pro.email FROM table1 AS usr, table2 AS pro WHERE usr.username = 'existing_username' AND / OR pro.email = 'existing_email'
I can't figure out how to write something like that. Basically I want it to return the username if it finds an existing one, and return the email if it finds one.
So it would return to me: username, email, both or none
Is this possible???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
WHERE 子句允许您指定
OR
。The WHERE clause allows you to specify
OR
.它不太清楚你想要什么。既然你没有加入表格,我假设你真的想要两者的并集
如果你想知道它来自哪里,你可以这样做
Its not really clear what you want. Since you're not joining the tables I'm assuming you really want the union of the two
If you want to know where it came from you could do
当然有可能。如果您有两个表,“用户名”包含所有已获取的用户名,“电子邮件”包含所有已获取的电子邮件,则此查询将返回具有相同用户名或相同电子邮件的所有行。
Certainly it's possible. If you have two tables, Usernames with all the taken user names, and Emails with all the taken emails, this query would return all the rows that either have the same username or the same email.
两张表还是一张表中的两列?
如果是后者,这行得通吗?
如果是前者,您可能需要编写两个查询,对照两个单独的列检查输入文本,并使用代码检查每个结果集的长度(如果 q1 为 1 或 q2 为 1),
除非两个表有一种 fk 关系,在这种情况下,您可以编写一个查询并使用 JOIN 语句。
Two tables or two columns in one table?
If the latter, would this work?
If it is the former, you would probably need to write two queries, checking the input text against the two separate columns and using the code to check the length of each result set (if q1 is 1 OR q2 is 1)
Unless the two tables have a fk relationship in which case you could write one query and use a JOIN statement.
我认为你应该为这个查询编写布尔逻辑......
参考这篇文章可能会对你有帮助。 布尔逻辑
i think you should write Boolean logic for this query....
refer this post may be it helps you. Boolean logic
或者,您可以将两个表的结果合并在一起:
如果表之间没有关系,则合并将为您提供结果。
Alternately, you could union results from both tables together:
The union will give you results if you don't have a relationship between the tables.