SQL:内部连接错误,可能很简单

时间:2016-04-25 12:32:46

标签: mysql sql database

我有2个具有相同列(id,country_code)的表,例如:

Table A
--------
id   country_code
1    fr
2    fr
3    fr

Table B
--------
id   country_code
1    ua
2    fr
3    uk

我想获取B中的所有字段,其中country_code与A中的每个字段不同,每个相同的ID,

预期的例子:

id  country_code
1   ua
3   uk

我尝试了内部联接,但没有任何成功,任何想法?

这是我得到的错误:

Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '<>'

但是在Workbench中,那些字段和表格具有相同的排序规则(设置为“表格默认值”),这很奇怪..

答案:我已经通过

检查了所有合作
show table status;

我更新了Collat​​ion列,现在它可以正常工作。

3 个答案:

答案 0 :(得分:4)

您可以使用join

select b.*
from b join
     a
     on b.id = a.id and b.country_code <> a.country_code;

答案 1 :(得分:2)

您需要使用Collat​​e关键字来更改排序规则:

dbcontext

有关整理的更多信息:What does character set and collation mean exactly?

答案 2 :(得分:0)

select b.id,b.country_code
from b 
left join a on b.country_code = a.country_code
where a.country_code is null 
相关问题