免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 7164 | 回复: 32
打印 上一主题 下一主题

请教 搞了一天没弄出来 数据的筛选 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-18 16:12 |只看该作者 |倒序浏览
一个表 :

-----------------------
列1    列2      列3
-----------------------
b    c        20
-----------------------
c    b        20
-----------------------
f    h        9
-----------------------
h    f        9
-----------------------

想要得到的结果:


-----------------------
列1    列2      列3
-----------------------
b    c        20
-----------------------
f    h       9
-----------------------

c  b  ,h  f  作为重复数据去除。如何写 sql 语句?高手帮忙。

[ 本帖最后由 Lning 于 2008-12-18 16:16 编辑 ]

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
2 [报告]
发表于 2008-12-18 16:17 |只看该作者
那f    h        20  这里怎么对应20呢??

论坛徽章:
0
3 [报告]
发表于 2008-12-18 16:23 |只看该作者
已经该了 结果应该是9 的

论坛徽章:
0
4 [报告]
发表于 2008-12-18 16:40 |只看该作者
有人麻烦解决下么  急!!!

论坛徽章:
0
5 [报告]
发表于 2008-12-18 16:55 |只看该作者
group by 一下就可以了。

论坛徽章:
0
6 [报告]
发表于 2008-12-18 16:58 |只看该作者
原帖由 yueliangdao0608 于 2008-12-18 16:55 发表
group by 一下就可以了。




貌似不是这样 ......继续

论坛徽章:
0
7 [报告]
发表于 2008-12-18 18:14 |只看该作者
select * from test t
  where exists (
                select 1 from test
                  where t.col3 = col3
                    and t.col1 = col2
                    and t.col2 = col1)
  and col1 < col2
希望有帮助

[ 本帖最后由 DQP 于 2008-12-18 23:03 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2008-12-18 18:24 |只看该作者
mysql> SELECT * FROM test;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| b    | c    |   20 |
| c    | b    |   20 |
| f    | h    |    9 |
| h    | f    |    9 |
+------+------+------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM test GROUP BY col3;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| f    | h    |    9 |
| b    | c    |   20 |
+------+------+------+
2 rows in set (0.37 sec)

mysql> SELECT * FROM test GROUP BY col3 ORDER BY col3 DESC;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| b    | c    |   20 |
| f    | h    |    9 |
+------+------+------+
2 rows in set (0.00 sec)

论坛徽章:
0
9 [报告]
发表于 2008-12-18 21:29 |只看该作者
挺难的。

论坛徽章:
0
10 [报告]
发表于 2008-12-19 09:16 |只看该作者
原帖由 DQP 于 2008-12-18 18:14 发表
select * from test t
  where exists (
                select 1 from test
                  where t.col3 = col3
                    and t.col1 = col2
                    and t.col2 = col1)
  ...



挺有帮助的  不过查询结果少了一行 还在研究  好了给出结果
自己写的
select test.* from test
join
(
select all_,max(fromcity) as cnt from
(
select test.*,tmp1.id1*tmp2.id2 as all_ from test
left join
(
select fromcity,
(select count(*) as id1 from
(select fromcity from test
union
select tocity from test)
tbl1 where tbl1.fromcity < tbl.fromcity ) as id1 from
(
select fromcity from test
union
select tocity from test
) tbl
) tmp1 on test.fromcity = tmp1.fromcity
left join
(
select fromcity,
(select count(*) as id2 from
(select fromcity from test
union
select tocity from test)
tbl1 where tbl1.fromcity < tbl.fromcity ) as id2 from
(
select fromcity from test
union
select tocity from test
) tbl
) tmp2 on test.tocity = tmp2.fromcity
) tmp3 group by all_
) tmp4 on test.fromcity = tmp4.cnt


顺便自己建立个测试用表 TEST,FROMCITY 和 TOCITY 是TEST表的其中两列
形式很像
----------------------
列1    列2      列3
-----------------------
b    c        20
-----------------------
c    b        20
-----------------------
f    h        9
-----------------------
h    f        9
-----------------------
用了衍生列的办法,生成一列ID号,然后将ID号相加或者相乘,得到重复的一列,然后用MAX函数选出其中一列,在进行JOIN 得到结果!
具体的顺序可以用MAX或者MIN来控制。

[ 本帖最后由 Lning 于 2008-12-19 09:27 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP