Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have the following query to return a bulk 'exists' query on multiple records. What I'm looking to do is only to select u.role and several other fields if the username exists in 'users'. This method works, but it's messy as I'm checking if the username exists for every column I'm selecting. Is there a more compact way to say;

If the username exists, return the role, x, y, z column etc.
Otherwise, return NULL for the role, x, y, z column etc.

CREATE TEMPORARY TABLE _users(username varchar(256));
        INSERT INTO _users VALUES('testuser1', 'testuser2');
        SELECT _u.username,
        CASE
                WHEN ISNULL(u.username) = 1 THEN 0
                ELSE 1
        END 'exists',
        CASE
                WHEN ISNULL(u.username) = 1 THEN u.role
                ELSE NULL
        END 'role'
        FROM _users _u
        LEFT JOIN users u
        ON u.username = _u.username;
        DROP TABLE _users


What I have tried:

-------------------------------------------------------------------------------
Posted
Updated 20-Mar-18 23:56pm
Comments
[no name] 21-Mar-18 17:50pm    
I’ve already said that the query works perfectly. I’m using MySQL, which only has one parameter for ISNULL.
Richard Deeming 21-Mar-18 17:54pm    
Where in your question did you mention MySQL?

You could at least have used the "MySQL" tag, rather than the generic "SQL" tag. MySQL has so many differences from regular SQL that it might as well be a different language.

1 solution

I'd recommend to read this: EXISTS (Transact-SQL)[^]

[EDIT]
If you would like to return NULL, you can use this trick:
SQL
SELECT TOP(1) Field1, Field2, Field3
FROM YourTable
WHERE UserName = 'Whatever'
UNION ALL
SELECT NULL AS Field1, NULL AS Field2, NULL As Field3


But - i do NOT recommend to do such a thing for several reason...

Here's a 'working' example:
SQL
DECLARE @t TABLE (uName VARCHAR(30))
INSERT INTO @t (uName)
VALUES('A'), ('B'), ('C')

SELECT TOP(1) uName
FROM @t
WHERE uName = 'D'
UNION ALL
SELECT NULL AS uName

--returns: NULL


[EDIT 2]
According to our discussion, a LEFT JOIN is doing exactly what you do expect to achieve. See:
DECLARE @tmp TABLE (uName VARCHAR(30))
INSERT INTO @tmp (uName)
VALUES('A'), ('B'), ('C')


DECLARE @orig TABLE (uName VARCHAR(30), [role] VARCHAR(30))
INSERT INTO @orig (uName, [role])
VALUES('A', 'admin'), ('D', 'user'), ('C', 'user')

SELECT t.uName AS TmpName, o.*
FROM @tmp t LEFT JOIN @orig o ON t.uName = o.uName 

Result:
TmpName	uName	role
A		A		admin
B		NULL	NULL
C		C		user


For further details, please see: Visual Representation of SQL Joins[^]
 
Share this answer
 
v6
Comments
[no name] 21-Mar-18 5:57am    
It's not the Exists part I'm concerned about, it's the logic.
Maciej Los 21-Mar-18 6:04am    
Check updated answer ;)
[no name] 21-Mar-18 6:10am    
No no no, you're not understanding what I'm asking for.

The query I have already works. The repeated 'CASE WHEN ISNULL(u.username) = 1 THEN' is messy, I don't want to have to do it for every column I want to return. I want one if statement, then if each of the usernames in the temporary table exists in 'users u', select u.role, x, y, z. If each one doesn't exist, return NULL, NULL, NULL, NULL
Maciej Los 21-Mar-18 6:23am    
See my answer again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900