SQL - Stumped on a SELECT - please help!

后端 未结 6 964
悲&欢浪女
悲&欢浪女 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:45

    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.

提交回复
热议问题