首页 > 解决方案 > 这些查询有什么区别

问题描述

问题 :

什么是差异......在这些查询中......当表是

我在这里使用了表别名我相信没有错误

------------------------ Tables -------------------
mysql> select * from college;
+----------+-------+------------+
| cName    | state | enrollment |
+----------+-------+------------+
| Stanford | CA    |      15000 |
| Berkeley | CA    |      36000 |
| MIT      | MA    |      10000 |
| Cornell  | NY    |      21000 |
+----------+-------+------------+
4 rows in set (0.00 sec)

mysql> select * from apply;
+-----+----------+----------------+----------+
| sID | cName    | major          | decision |
+-----+----------+----------------+----------+
| 123 | Berkeley | CS             | Y        |
| 123 | Cornell  | EE             | Y        |
| 123 | Stanford | CS             | Y        |
| 123 | Stanford | EE             | N        |
| 234 | Berkeley | biology        | N        |
| 345 | Cornell  | bioengineering | N        |
| 345 | Cornell  | CS             | Y        |
| 345 | Cornell  | EE             | N        |
| 345 | MIT      | bioengineering | Y        |
| 543 | MIT      | CS             | N        |
| 678 | Stanford | history        | Y        |
| 765 | Cornell  | history        | N        |
| 765 | Cornell  | psychology     | Y        |
| 765 | Stanford | history        | Y        |
| 876 | MIT      | biology        | Y        |
| 876 | MIT      | marine biology | N        |
| 876 | Stanford | CS             | N        |
| 987 | Berkeley | CS             | Y        |
| 987 | Stanford | CS             | Y        |
+-----+----------+----------------+----------+
19 rows in set (0.00 sec)

----------------- 2个几乎相同的查询:但不同。输出 -----------------

mysql> Select c.cname from College c,Apply a where  c.cname=a.cname  and enrollment > 20000 and major='CS';
+---------+
| cname   |
+---------+
| Cornell |
+---------+
1 row in set (0.00 sec)

  ----------------------> only One row

mysql>
mysql> select college.cname from college,apply where college.cname = apply.cname and enrollment > 20000 and major = 'CS';
+----------+
| cname    |
+----------+
| Berkeley |
| Cornell  |
| Berkeley |
+----------+
3 rows in set (0.00 sec)

 WHILE -------------------------> three rows

标签: mysql

解决方案


你有 2 个不同的表 College 和 College,所以你得到不同的输出。如果您使用 College 而不是 College,那么您将获得相同的输出。


推荐阅读