Mysql:使用=运算符进行字符串比较

Mysql: String comparison using = operator

我是 mysql 的新手,现在正在学习连接查询。当我比较字符串时,我得到了下面提到的奇怪输出。我有两张桌子

   MariaDB [test]> select * from classroom;
    +---------+-----------+
    | subject | classroom |
    +---------+-----------+
    | maths   |         1 |
    | englishs|         2 |
    +---------+-----------+

Table 学生:

  MariaDB [test]> select * from student;
  +------+------+---------+
  | id   | name | subject |
  +------+------+---------+
  |    1 | abc  | maths   |
  |    2 | abcd | english |
  +------+------+---------+

我试过这个查询

 select b.classroom,a.name,b.subject from student a left join classroom b
 on a.subject = b.subject ;

输出就像,

  +-----------+------+---------+
  | classroom | name | subject |
  +-----------+------+---------+
  |         1 | abc  | maths   |
  |      NULL | abcd | NULL    |
  +-----------+------+---------+

如果两个表中的字符串不匹配,我不明白为什么我会得到第二行。

这与字符串比较无关。

您正在使用 外连接,但您期望的结果是 内连接 给出的结果。

查看 this post 了解有关内部和外部联接的详细解释。

来自post:

  • An inner join of A and B gives the result of A intersect B, i.e. the inner part of a Venn diagram intersection.

  • An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagram union.

尝试这可能会奏效。

 select b.classroom,a.name,b.subject from student a,classroom b where a.subject = b.subject