hyljava
导航
BlogJava
首页
新随笔
联系
聚合
管理
<
2014年3月
>
日
一
二
三
四
五
六
23
24
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
统计
随笔 - 110
文章 - 1
评论 - 47
引用 - 0
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(3)
给我留言
查看公开留言
查看私人留言
随笔分类
AJAX(3)
(rss)
Android(10)
(rss)
css(1)
(rss)
eclipse(1)
(rss)
eclipse(1)
(rss)
Flex(4)
(rss)
Hibernate(7)
(rss)
HTML5(6)
(rss)
java(19)
(rss)
JS(22)
(rss)
Jsp(14)
(rss)
JSTL(4)
(rss)
Linux(3)
(rss)
MySQL(7)
(rss)
Oracle(6)
(rss)
Spring(3)
(rss)
spring(5)
(rss)
Spring MVC(6)
(rss)
SQL Server(4)
(rss)
Struts1(6)
(rss)
Struts2(6)
(rss)
SVN(1)
(rss)
Tomcat(2)
(rss)
哲理故事与管理之道(13)
(rss)
广禾养老文化村(1)
(rss)
随笔档案
2017年3月 (1)
2016年4月 (1)
2015年10月 (1)
2015年9月 (2)
2015年8月 (3)
2015年5月 (1)
2015年3月 (1)
2014年8月 (1)
2014年5月 (1)
2014年4月 (1)
2014年3月 (1)
2014年2月 (4)
2014年1月 (1)
2013年12月 (6)
2013年11月 (2)
2013年9月 (1)
2013年7月 (1)
2013年5月 (3)
2013年4月 (15)
2013年3月 (7)
2013年2月 (9)
2013年1月 (3)
2012年12月 (1)
2012年11月 (4)
2012年10月 (1)
2012年9月 (3)
2012年8月 (3)
2012年7月 (6)
2012年6月 (2)
2012年5月 (17)
2012年4月 (5)
文章分类
Hibernate(1)
(rss)
Struts1.2
(rss)
SVN
(rss)
文章档案
2012年4月 (1)
相册
me
搜索
最新评论
1. re: Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允
好
--好
2. re: Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允
@好
好好好
--好好
3. re: 利用百度地图的API定位地理位置
你这根本不是定位呀,哥们,你那个经纬度是写死的,只是给这个经纬度添加标注而已
--罗
4. re: 监听键盘按下键的值(兼容IE与其他浏览器)
asdf
--asdfasdfasdf
5. re: 利用百度地图的API定位地理位置
未完全
--我企鹅完全
阅读排行榜
1. Spring中的JDBCTemplate使用(非常详细的配置过程)(38325)
2. c:forEach的varStatus属性 (31505)
3. Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允(31458)
4. 利用百度地图的API定位地理位置(21213)
5. Tomcat的跨区域访问(6322)
评论排行榜
1. 升级版JDBC工具类(10)
2. 利用百度地图的API定位地理位置(4)
3. Spring MVC级联查询(用户与地址之间关系)(3)
4. ajax的传值(2)
5. Write operations are not allowed in read-only mode 只读模式下(FlushMode.NEVER/MANUAL)写操作不允(2)
mysql 解决全连接问题
基本资料:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.0.16 |
+-----------+
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
+----+------+
mysql> select * from t2;
+------+-------+
| code | price |
+------+-------+
| 1 | 100 |
| 3 | 900 |
| 5 | 2500 |
+------+-------+
内连,左连,右连都正确:
mysql> select * from t1 inner join t2 on t1.id = t2.code;
+----+------+------+-------+
| id | name | code | price |
+----+------+------+-------+
| 1 | aa | 1 | 100 |
| 3 | cc | 3 | 900 |
+----+------+------+-------+
mysql> select * from t1 left join t2 on t1.id = t2.code
+----+------+------+-------+
| id | name | code | price |
+----+------+------+-------+
| 1 | aa | 1 | 100 |
| 2 | bb | NULL | NULL |
| 3 | cc | 3 | 900 |
+----+------+------+-------+
mysql> select * from t1 right join t2 on t1.id = t2.code;
+------+------+------+-------+
| id | name | code | price |
+------+------+------+-------+
| 1 | aa | 1 | 100 |
| 3 | cc | 3 | 900 |
| NULL | NULL | 5 | 2500 |
+------+------+------+-------+
全连有错:
FULL JOIN 错误一:
第一个表名不能出现在on的关联关系中,例如:
mysql> select * from t1 full join t2 on t1.id = t2.code;
ERROR 1054 (42S22): Unknown column 't1.id' in 'on clause'
语句更改为如下后运行不再报错:
mysql> select * from t1 full join t2 on id = t2.code;
或者:
mysql> select * from t1 full join t2 on id = code;
FULL JOIN 错误二:
上面语句运行后,结果如下:
+----+------+------+-------+
| id | name | code | price |
+----+------+------+-------+
| 1 | aa | 1 | 100 |
| 3 | cc | 3 | 900 |
+----+------+------+-------+
显然,这不是正确的full join结果集,而是inner join的结果集。替代方案如下:
mysql> select * from t1 left join t2 on id = code union select * from t1 right join t2 on id = code;
+------+------+------+-------+
| id | name | code | price |
+------+------+------+-------+
| 1 | aa | 1 | 100 |
| 2 | bb | NULL | NULL |
| 3 | cc | 3 | 900 |
| NULL | NULL | 5 | 2500 |
+------+------+------+-------+
INTERSECT错误:
mysql> select * from t1 left join t2 on id = code intersect select * from t1 right join t2 on id = code;
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 'intersect select * from t1 right join t2 on id = code' at line 1
说明MySQL不支持INTERSECT。替代方案如下:
mysql> select * from t1 inner join t2 on id = code;
+----+------+------+-------+
| id | name | code | price |
+----+------+------+-------+
| 1 | aa | 1 | 100 |
| 3 | cc | 3 | 900 |
+----+------+------+-------+
MINUS错误:
mysql> select * from t1 left join t2 on id = code minus select * from t1 right join t2 on id = code;
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 'minus select * from t1 right join t2 on id = code' at line 1
说明MySQL不支持MINUS。
而在MS SQL Server中运行(select * from t1 left join t2 on id = code except select * from t1 right join t2 on id = code;)可以得到期望结果如下:
+------+------+------+-------+
| id | name | code | price |
+------+------+------+-------+
| 2 | bb | NULL | NULL |
+------+------+------+-------+
运行如下语句:
mysql> select * from t1 left join t2 on id = code where (id,name,code,price) not in (select * from t1 right join t2 onid = code);
Empty set (0.00 sec)
或者:
mysql> select id,name,code,price from t1 left join t2 on id = code where (id,name,code,price) not in (select id,name,code,price from t1 left join t2 on id = code);
Empty set (0.00 sec)
都没有得到期望中的结果。
(是否因为有NULL值字段,无法用IN 和 NOT IN 来匹配啦?菜鸟在此抛砖引玉。)
换用下面语句检验用 NOT IN 替代 MINUS,能得出期望结果:
mysql> select * from t1,t2 where (id,name,code,price) not in (select * from t1,t2 where id = code);
+----+------+------+-------+
| id | name | code | price |
+----+------+------+-------+
| 2 | bb | 1 | 100 |
| 3 | cc | 1 | 100 |
| 1 | aa | 3 | 900 |
| 2 | bb | 3 | 900 |
| 1 | aa | 5 | 2500 |
| 2 | bb | 5 | 2500 |
| 3 | cc | 5 | 2500 |
+----+------+------+-------+
可以看到这是广义笛卡尔积减去INNER JOIN的结果集。
posted on 2014-03-03 19:30
何云隆
阅读(338)
评论(0)
编辑
收藏
所属分类:
MySQL
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
相关文章:
解决SoapUI的Request URL不支持大写
SQL按照汉字排序
mysql 解决全连接问题
升级版JDBC工具类
T-SQL语句创建触发器
执行MysqL命令出现中文显示乱码
Powered by:
BlogJava
Copyright © 何云隆