SQL - Stumped on a SELECT - please help!

后端 未结 6 1083
悲&欢浪女
悲&欢浪女 2021-01-27 17:52

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 = \'ex         


        
6条回答
  •  不要未来只要你来
    2021-01-27 18:46

    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'
    

提交回复
热议问题