简体   繁体   中英

Query is not returning any rows with like '%' operator and left outer join, if right table has no rows

I am using MS SQL SERVER 2008R2 . i have two tables A and B as

create table A(
id int primary key, name Varchar(20));

create table B(
id int primary key, user_name Varchar(20));

insert into A values(1,'A1');
insert into A values(2,'A2');
insert into A values(3,'A3');
insert into A values(4,'A4');
insert into A values(5,'A5');

Now my problem is :

select A.* 
from A left outer join B on A.id = B.id 
where B.user_name like '%';

or

select A.* 
from A left outer join B on A.id = B.id 
where B.user_name like '%%';

Above written query does not return any records even though left table have 5 entries in it. without any filter on right table it works fine.

select A.* from A left outer join B on A.id = B.id 

this query will give you out put like this...

id  name    id  user_name
1   A1  NULL    NULL
2   A2  NULL    NULL
3   A3  NULL    NULL
4   A4  NULL    NULL
5   A5  NULL    NULL

and you are comparing username using like with null

select A.* from A left outer join B on A.id = B.id where B.user_name like '%%';

hence it will not give you any output

you should try following query

select A.*,b.* from A left outer join B on A.id = B.id where (b.user_name like '%%' or b.user_name is null)

In your scenario...first left join is happening it is finding 5 entries and then on that record set sql sever is applying filter of user_name and as user_name for all rows is null..no records are getting displayed.

you can change your query to

select A.* from A left outer join B on A.id = B.id where ISNULL(B.user_name,'') like '%%';

您正在使用通配符来比较空值,使用此,

SELECT a.* FROM   a LEFT OUTER JOIN b ON a.id = b.id WHERE  b.user_name LIKE '%' OR b.user_name IS NULL;  

Since all values in table B are NULL , any wildcard match on NULL values will return NULL .

So the condition where B.user_name like '%'; translates into where NULL like '%'; which evaluates to NULL as NULL cannot be compared with any value.

 select A.* from A left outer join B on 
 A.id = B.id where COALESCE(B.user_name,'') like '%%';

 select A.* from A left outer join B on 
 A.id = B.id where COALESCE(B.user_name,'') like '%';

supporting sql fiddle : http://sqlfiddle.com/#!6/1ca91/8

Note that the COALESCE is ANSI, and therefore supported in Oracle, SQL Server and PostGres and does shortcut evaluation. n

Edit: Based on new information that this same query should work in all SQL Server, PostGres and Oracle. I am changing the SQL query to use COALESCE instead which is supported in all

Unless you use ISNULL() and check like this where ISNULL(B.user_name,'') like '%';

 select A.* from A left outer join B on A.id = B.id where ISNULL(B.user_name,'') like '%%'; select A.* from A left outer join B on A.id = B.id where ISNULL(B.user_name,'') like '%'; 

See this fiddle http://sqlfiddle.com/#!6/1ca91/6

Please try this one:

select A.* from A
left outer join (SELECT * FROM B where user_name like '%') X on A.id = X.id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM