[英]mysql left outer join exclude
I have 2 tables ' conta
' and ' details
' and both the tables have null values and data in different case 我有2个表'
conta
'和' details
',并且两个表在不同情况下都具有空值和数据
conta
id col1 col2 col3 col4
1 Hi Bye See YOU
2 Hello (NULL) (NULL) (NULL)
details
id new_column1 new_column2 new_column3
1 bye see you
I want to apply join based on col2=new_column1 and col3 = new_column2 and col4 = new_column3
and get the values that are present in conta
and not in details
, so my output will be 我想根据申请加入
col2=new_column1 and col3 = new_column2 and col4 = new_column3
和获取存在于价值conta
而不是details
,所以我的输出将是
conta
id col1 col2 col3 col4
2 hello (NULL) (NULL) (NULL)
But somehow i am unable to do so. 但是我无法做到这一点。 I wrote below query, but its simply not resulting me the values i want.
我在下面的查询中编写了代码,但它根本无法为我提供所需的值。
SELECT `id`,`col1`,`col2`,`col3`,`col4` FROM `conta`
WHERE LOWER(`col2`) + LOWER(`col3`) + LOWER(`col4`) NOT IN (SELECT DISTINCT(LOWER(`new_column1`) + LOWER(`new_column2`) + LOWER(`new_column3`))
FROM `details`);
It simply give me no results! 它根本没有结果! in the display
在显示器上
Any help? 有什么帮助吗?
Edit: I tried below query as suggested by @Uueerdo and it isn't giving me what i want. 编辑:我尝试了以下查询,如@Uueerdo所建议,它没有给我我想要的东西。
SELECT conta.id,`col1`,`col2`,`col3`,`col4` FROM `conta`
LEFT OUTER JOIN `details`
ON ((conta.col2 IS NULL AND details.new_column1 IS NULL)
OR (LOWER(conta.col2) = LOWER(details.new_column1)))
AND ((conta.col3 IS NULL AND details.new_column2 IS NULL)
OR (LOWER(conta.col3) = LOWER(details.new_column2)))
AND ((conta.col4 IS NULL AND details.new_column3 IS NULL)
OR (LOWER(conta.col4) = LOWER(details.new_column3)))
WHERE details.id IS NULL
In the output in col2
i see a value ' Operations ' which is also present in new_column1
in details
table. 在
col2
的输出中,我看到一个值“ Operations ”,该值也出现在details
表的new_column1
中。 This means it shouldn't be present in the output as i am trying to apply left outer join exclude I even tried using LEFT JOIN instead of LEFT OUTER JOIN and it isn't working either 这意味着它不应该出现在输出中,因为我尝试应用左外部联接,我什至尝试使用LEFT JOIN代替LEFT OUTER JOIN,并且它也不起作用
Edit2 : I found the solution. Edit2 :我找到了解决方案。 the query works and does the job.
查询工作并完成工作。 Exept that i had to run a command to replace all blank cells in the columns where i am applying join to NULL values.
举例来说,我必须运行一个命令来替换我将Join应用于NULL值的列中的所有空白单元格。
You're better off using a SELECT .... FROM a LEFT JOIN b ON conditions WHERE b.id IS NULL
style of query; 您最好使用
SELECT .... FROM a LEFT JOIN b ON conditions WHERE b.id IS NULL
样式的查询SELECT .... FROM a LEFT JOIN b ON conditions WHERE b.id IS NULL
; null comparisons are a little different (and can be handled join conditions). 空比较略有不同(可以处理联接条件)。
For example these evaluate to NULL, which is not true, which is false: 例如,这些计算结果为NULL,这不是true,这是false:
But you can do things like this to compare nulls more easily: 但是您可以执行以下操作以更轻松地比较null:
So you're join condition can be something like: 因此,您的加入条件可以是:
[...]
ON ((conta.col2 IS NULL AND details.new_column1 IS NULL)
OR (LOWER(conta.col2) = LOWER(details.new_column1)))
AND ((conta.col3 IS NULL AND details.new_column2 IS NULL)
OR (LOWER(conta.col3) = LOWER(details.new_column2)))
[and so on...]
WHERE details.id IS NULL
This assumes details
has some sort of non-null row identification field that can used to reliably determine if there was a match or not. 假设
details
具有某种非空行标识字段,可用于可靠地确定是否存在匹配项。
Edit: The precise problem with your current query (aside from the null issues I previously outlined) is that +
is not concatenation in MySQL, it is addition. 编辑:当前查询的确切问题(除了我之前概述的空问题之外)是
+
不是MySQL中的串联,而是加法。 With the data you've shown both LOWER(col2) + LOWER(col3) + LOWER(col4)
and LOWER(new_column1) + LOWER(new_column2) + LOWER(new_column3)
with yield 0
for the rows without NULL values in them. 对于数据,您已经为其中没有NULL值的行显示了
LOWER(col2) + LOWER(col3) + LOWER(col4)
和LOWER(new_column1) + LOWER(new_column2) + LOWER(new_column3)
且产量为0
。 You would need to use the CONCAT()
function to do the operation instead; 您将需要使用
CONCAT()
函数代替该操作; but I'd discourage it because CONCAT('abc', 'def', '')
is equal to CONCAT('ab', 'cd', 'ef')
. 但我不建议这样做,因为
CONCAT('abc', 'def', '')
等于CONCAT('ab', 'cd', 'ef')
。
Sidenote: DISTINCT
is not a function, the ()
will have no effect ( other than that they would cause a problem if they contained more than one result field ). 旁注:
DISTINCT
不是函数, ()
将无效( 除非它们包含多个结果字段会导致问题 )。
You can keep your general format, and the aforementioned null issues, by a simple change with this format : WHERE (a, b, c) IN (SELECT a, b, c FROM ....
您可以使用以下格式进行简单的更改来保留常规格式以及上述空问题:
WHERE (a, b, c) IN (SELECT a, b, c FROM ....
You can do a left join, then test for a null in the 2nd table to find the rows in the first table that didn't match any rows in the 2nd table. 您可以进行左联接,然后在第二张表中测试是否为空,以查找第一张表中与第二张表中任何行都不匹配的行。
SELECT
a.`id`,
a.`col1`,
a.`col2`,
a.`col3`,
a.`col4`
FROM `conta` a
LEFT JOIN `details` b
ON a.`col2` like b.`new_column1`
AND a.`col3` like b.`new_column2`
AND a.`col4` like b.`new_column3`
WHERE b.`id` IS NULL
You could use the EXISTS
operator to create an anti-semi join. 您可以使用
EXISTS
运算符创建反半联接。 Please take a look at this link: https://www.techonthenet.com/mysql/exists.php 请查看以下链接: https : //www.techonthenet.com/mysql/exists.php
Example Query: 查询示例:
SELECT
id,
col1,
col2,
col3,
col4
FROM conta
WHERE NOT EXISTS (
SELECT 1
FROM details
WHERE conta.col2 LIKE details.new_column1
AND conta.col3 LIKE details.new_column2
AND conta.col4 LIKE details.new_column3
)
Your Problem Solution Is inner join your (contact table) with (details table) and get (contact.*) All columns where Contact.col1 !=details.new_column1 您的问题解决方案是将(联系表)与(详细信息表)内部连接,并获得(contact。*)所有列,其中Contact.col1!= details.new_column1
here is a query 这是一个查询
Select conta.* from conta inner join details on conta.col1!=details.new_column1
You Can And More Where column in inner join 您可以并且在内部联接中的更多位置列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.