SQL-被困在SELECT上-请帮忙! [英] SQL - Stumped on a SELECT - please help!

查看:98
本文介绍了SQL-被困在SELECT上-请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SQL Server 2005中实现以下目标:

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.

因此它将返回给我:用户名,电子邮件,或者两者都不输入

这可能吗?

推荐答案

尚不清楚您想要什么.由于您没有加入表格,因此我假设您确实想要两者的结合

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'

如果您想知道它的来源

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'

这篇关于SQL-被困在SELECT上-请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆