【发布时间】:2015-06-19 07:09:59
【问题描述】:
给定客户表和贷款表,我希望找到没有贷款的客户。架构定义如下:
mysql> select * from customer;
+---------------+-----------------+---------------+
| customer_name | customer_street | customer_city |
+---------------+-----------------+---------------+
| Adams | Spring | Pittsfield |
| Brooks | Senator | Brooklyn |
| Curry | North | Rye |
| Glenn | Sand Hill | Woodside |
| Green | Walnut | Stamford |
| Hayes | Main | Harrison |
| Johnson | Alma | Palo Alto |
| Jones | Main | Harrison |
| Lindsay | Park | Pittsfield |
| Smith | North | Rye |
| Turner | Putnam | Stamford |
| Williams | Nassau | Princeton |
+---------------+-----------------+---------------+
12 rows in set (0.00 sec)
mysql> select * from borrower;
+---------------+---------+
| customer_name | loan_id |
+---------------+---------+
| Adams | L16 |
| Curry | L93 |
| Hayes | L15 |
| Jackson | L14 |
| Jones | L17 |
| Smith | L11 |
| Smith | L23 |
| Williams | L17 |
| Adams | L19 |
| Adams | L15 |
| Jones | L15 |
| Williams | L23 |
+---------------+---------+
12 rows in set (0.00 sec)
现在,我尝试了查询:select customer_name from customer except select customer_name from borrower;
但我得到了错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select customer_name from borrower' at line 1
我也尝试了difference,但弹出了同样的错误。但是union 效果很好。
mysql> select customer_name from customer union select customer_name from borrower;
+---------------+
| customer_name |
+---------------+
| Adams |
| Brooks |
| Curry |
| Glenn |
| Green |
| Hayes |
| Johnson |
| Jones |
| Lindsay |
| Smith |
| Turner |
| Williams |
| Jackson |
+---------------+
13 rows in set (0.00 sec)
有什么我可能错的建议吗?
【问题讨论】:
-
MySQL 不支持
EXCEPT语法。 -
我认为这是一个重复的问题,试试这个链接stackoverflow.com/questions/16092353/…