Need help with SQL query - out join solution?
I have a database table of email addresses. I want to query it based on a set of strings.
I do not just want the query results to return those email addresses contained in the set of strings - that would be simple.
Rather, I want to get back all of the strings used in the query, along with a Boolean field that tells me if it is i开发者_Go百科n the table or not. e.g.: joe@joe.com TRUE sam@sam.com TRUE jim@jim.com FALSE
I have on solution (joining an 'In' query with a 'Not In' query) - but I was hoping for something better.
(Yes, I realize I can figure it out without the database by doing the simple query then removing the found items from the set of query strings)
Put the addresses you are querying for into a (temporary) table, then LEFT JOIN the email address table to it.
SELECT
r.email_address,
CASE WHEN e.email_address IS NULL THEN 'FALSE' ELSE 'TRUE' END AS is_present
FROM requested_emails r
LEFT JOIN email_addresses e ON r.email_address = e.email_address
declare @CrappyStr varchar(10)
set @CrappyStr = 'dummy'
select @CrappyStr AS SearchString
, email_address
, CASE WHEN charindex(@CrappyStr, email_address) > 0 THEN 'True'
ELSE 'False' END AS [BOO-lean]
from SnarkyTable
Note that SQL does not have a boolean type. Here I'm using a string.
SELECT email_address, NVL( sq.present, 'FALSE' ) AS present
FROM mytable t
LEFT JOIN ( SELECT email_address, 'TRUE' AS present
FROM mytable
WHERE email_address in ( ... your list here ... )
) sq ON sq.email_address = t.email_address
精彩评论