SQL - 被 SELECT 困扰 - 请帮忙!

发布于 2024-10-09 05:44:31 字数 334 浏览 2 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最终幸福 2024-10-16 05:44:31

WHERE 子句允许您指定 OR

SELECT username, email FROM table WHERE username = @username OR email = @email

The WHERE clause allows you to specify OR.

SELECT username, email FROM table WHERE username = @username OR email = @email
﹏半生如梦愿梦如真 2024-10-16 05:44:31

它不太清楚你想要什么。既然你没有加入表格,我假设你真的想要两者的并集

SELECT 
       usr.UserName foo
FROM
     table1 AS usr
WHERE
    usr.username = 'existing_username'
UNION ALL SELECT 
       pro.email foo
FROM
     table2 AS pro
WHERE
    pro.email = 'existing_email'

如果你想知道它来自哪里,你可以这样做

SELECT 
       usr.UserName foo,
       'usr' source
FROM
     table1 AS usr
WHERE
    usr.username = 'existing_username'
UNION SELECT 
       pro.email foo
      'email' source
FROM
     table2 AS pro
WHERE
    pro.email = 'existing_email'

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

SELECT 
       usr.UserName foo
FROM
     table1 AS usr
WHERE
    usr.username = 'existing_username'
UNION ALL SELECT 
       pro.email foo
FROM
     table2 AS pro
WHERE
    pro.email = 'existing_email'

If you want to know where it came from you could do

SELECT 
       usr.UserName foo,
       'usr' source
FROM
     table1 AS usr
WHERE
    usr.username = 'existing_username'
UNION SELECT 
       pro.email foo
      'email' source
FROM
     table2 AS pro
WHERE
    pro.email = 'existing_email'
烟花易冷人易散 2024-10-16 05:44:31

当然有可能。如果您有两个表,“用户名”包含所有已获取的用户名,“电子邮件”包含所有已获取的电子邮件,则此查询将返回具有相同用户名或相同电子邮件的所有行。

SELECT * FROM Usernames, Emails WHERE Usernames.Name = 'username' OR Emails.Email = 'email'

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.

SELECT * FROM Usernames, Emails WHERE Usernames.Name = 'username' OR Emails.Email = 'email'
为你拒绝所有暧昧 2024-10-16 05:44:31

两张表还是一张表中的两列?

如果是后者,这行得通吗?

"SELECT * from the_table WHERE email = '".$input_text."' OR username = '".$input_text."'";

如果是前者,您可能需要编写两个查询,对照两个单独的列检查输入文本,并使用代码检查每个结果集的长度(如果 q1 为 1 或 q2 为 1),

除非两个表有一种 fk 关系,在这种情况下,您可以编写一个查询并使用 JOIN 语句。

Two tables or two columns in one table?

If the latter, would this work?

"SELECT * from the_table WHERE email = '".$input_text."' OR username = '".$input_text."'";

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.

清晰传感 2024-10-16 05:44:31

我认为你应该为这个查询编写布尔逻辑......

参考这篇文章可能会对你有帮助。 布尔逻辑

i think you should write Boolean logic for this query....

refer this post may be it helps you. Boolean logic

私野 2024-10-16 05:44:31
select usr.username, pro.email
from table1 as usr
inner join table2 as pro on
  usr.id = pro.userId --I'm guessing on the relationship here
where 
  usr.usrname = 'existing_username' or
  pro.email = 'existing_email'

或者,您可以将两个表的结果合并在一起:

(select username, null as email from table1)
union
(select null as username, email from table2)

如果表之间没有关系,则合并将为您提供结果。

select usr.username, pro.email
from table1 as usr
inner join table2 as pro on
  usr.id = pro.userId --I'm guessing on the relationship here
where 
  usr.usrname = 'existing_username' or
  pro.email = 'existing_email'

Alternately, you could union results from both tables together:

(select username, null as email from table1)
union
(select null as username, email from table2)

The union will give you results if you don't have a relationship between the tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文