[英]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.我有两个要离开连接的表,A 和 B。但引用只显示一个条件。
TABLE A表A
id | name
1 | name1
2 | name2
3 | name3
4 | name4
5 | name5
TABLE B表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)由“id”列和 B 表链接的表只有 3 行,我想显示所有 A 表记录及其值(如果 B 表中没有具有相同 id 的记录,则为 NULL)
this is my sql query:这是我的 sql 查询:
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:它显示了 A 表的所有行,但所有值都是 NULL:
I want to show all A table's record which value above 0我想显示所有 A 表的记录,哪个值大于 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: SQLFiddle 在尝试构建时似乎遇到了问题,但给出了以下内容:
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
和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. table A
应仅加入 ID 匹配且value
<= 1 的table b
。在您提供的示例数据中,唯一具有 <= 1 值的行是 ID 4。因此,为什么 ID 4 是查询中输出的唯一value
- 它是table B
唯一符合连接条件的行。
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.请注意,上面是
sql-server
作为您帖子中指示的原始标签之一 - 看起来它实际上是mysql
但相同的想法仍然适用。
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.请注意,唯一的变化是我
AND B.value<=1
了AND B.value<=1
- 我不知道为什么要包含它,因为它会将您的结果集限制为您不想要的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.