Home > Database > Mysql Tutorial > body text

Oracle 查询不重复多列SQL写法

WBOY
Release: 2016-06-07 17:21:57
Original
1326 people have browsed it

Oracle中要求查询表customer 中 t.address , t.customer_name不重复的列:

Oracle中要求查询表customer 中 t.address , t.customer_name不重复的列:

通常想法是:

select distinct t.address , t.customer_name from customer t 

然而这种写法在oracle是错误的。

因此有第二种想法:

select count(*) from ( select distinct t.address , t.customer_name from customer t) 

这种写法是正确的,然而有没有更好的写法呢

突发奇想的第三种,充分利用了||的连接功能:

select  count(distinct t.address ||t.customer_name) from  customer t 

这样不就ok了吧,其实不是的,看看下面这种情况就知道了

若第一条记录为:

address= testAddT ,customer_name=omcat 

第二条记录

address= testAdd ,customer_name=Tomcat 

这种情况t.address ||t.customer_name得出的值都是一样的,然而显然这两条记录是不同的,如何解决这种问题呢,,就是加入特殊字符来解决,比如我们确定这两列字段中不会出现#这样的字符内容,好办,此时就可以写出如下完美的sql语句了

select  count(distinct t.address ||'#'||t.customer_name) from  customer t 

最后说明:不同的数据库中的sql查询语法都可能都会有差别的所以要针对特定数据库而言,不过思想是可以借鉴的,因此重要的是理解灵活的解决问题思想。

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!