简体   繁体   中英

LEFT JOIN TABLE WITH TWO CONDITION IN MYSQL

I have two tables that i want to left join, A and B. But the referrence only show with one condition.

TABLE A

 id | name
  1 | name1
  2 | name2
  3 | name3
  4 | name4
  5 | name5

TABLE B

id | value
 1 | 2
 2 | 4
 4 | 0

the table linked by "id" column and B table only have 3 rows, I want to show all of the A table record and its value (it NULL if no record with same id in B table)

this is my sql query:

SELECT A.*, B.* 
FROM A LEFT JOIN B 
ON A.id=B.id AND B.value<=1 
ORDER BY A.id;

it shows all of A table rows, but all the value is NULL:

I want to show all A table's record which value above 0

Like this:

id  | name  | value
1   | name1  | 2
2   | name2   | 4
3   | name3   | NULL
4   | name4   | 0
5   | name5   | NULL

SQLFiddle is seemingly having issues when trying to build one, but given the following:

create table #tablea (id int, name varchar(50))
insert into #tablea (id, name)
select 1, 'name1'
union all 
select 2, 'name2'
union all 
select 3, 'name3'
union all 
select 4, 'name4'
union all 
select 5, 'name5'

create table #tableb (id int, value int)
insert into #tableb (id, value)
select 1, 2
union all 
select 2, 4
union all
select 4, 0

SELECT A.*, B.* 
FROM #tablea A LEFT JOIN #tableb B 
ON A.id=B.id AND B.value<=1 
ORDER BY A.id;

DROP TABLE #tablea
DROP TABLE #tableb

Your posted query:

SELECT A.*, B.* 
FROM #tablea A 
LEFT JOIN #tableb B 
ON A.id=B.id AND B.value<=1 
ORDER BY A.id;

would return:

id  name    id      value
----
1   name1   NULL    NULL
2   name2   NULL    NULL
3   name3   NULL    NULL
4   name4   4       0
5   name5   NULL    NULL

not all nulls as you stated. The results it has returned are correct with the query as you've written it.

SELECT A.*, B.* 
FROM #tablea A 
LEFT JOIN #tableb B 
ON A.id=B.id AND B.value<=1 
ORDER BY A.id;

The above stated, select all columns from table a and table b . table A should only join onto table b where the IDs match and the value is <= 1. In the sample data you have provided, the only row that has a value of <= 1, is ID 4. Hence, why ID 4 is the only value being output in your query - it's the only row from table B that meets your join criteria.

Now this is only an explanation of why you're getting what you're getting. It is still not clear to me what you want to get.

Note the above is sql-server as one of the original tags in your post indicated - looks like it's actually mysql but the same ideas still apply.

Based on your edit, and the data you actually want, just change your query to this:

SELECT A.*, B.* 
FROM A LEFT JOIN B 
ON A.id=B.id
ORDER BY A.id;

note, the only change is i took out AND B.value<=1 - I don't have an idea why this was included as it's limiting your result set to something you don't want.

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