distinct

MySQL SELECT DISTINCT should be case sensitive?

∥☆過路亽.° 提交于 2019-12-29 04:43:20
问题 How do I make MySQL's SELECT DISTINCT case sensitive? create temporary table X (name varchar(50) NULL); insert into X values ('this'), ('This'); Now this query: select distinct(name) from X; Results in: this What's going on here? I'd like SELECT DISTINCT to be case sensitive. Shouldn't that be the default? 回答1: Use BINARY operator for that: SELECT DISTINCT(BINARY name) AS Name FROM X; You can also CAST it while selecting: SELECT DISTINCT (CAST(name AS CHAR CHARACTER SET utf8) COLLATE utf8_bin

GROUP BY and COUNT in PostgreSQL

爷,独闯天下 提交于 2019-12-29 03:33:47
问题 The query: SELECT COUNT(*) as count_all, posts.id as post_id FROM posts INNER JOIN votes ON votes.post_id = posts.id GROUP BY posts.id; Returns n records in Postgresql: count_all | post_id -----------+--------- 1 | 6 3 | 4 3 | 5 3 | 1 1 | 9 1 | 10 (6 rows) I just want to retrieve the number of records returned: 6 . I used a subquery to achieve what I want, but this doesn't seem optimum: SELECT COUNT(*) FROM ( SELECT COUNT(*) as count_all, posts.id as post_id FROM posts INNER JOIN votes ON

Using Hibernate's Criteria and Projections to Select Multiple Distinct Columns

為{幸葍}努か 提交于 2019-12-28 12:15:27
问题 Using Hibernate's Criteria, I want to execute the equivalent of: select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON' I thought doing the following would yield the results I wanted: ProjectionList projList = new ProjectionList(); projList.add(Projections.distinct(Projections.property("id.state"))); projList.add(Projections.distinct(Projections.property("id.uspsCity"))); criteria.setProjection(projList); But, what this actually does is execute something like: select

mongo中的高级查询之聚合操作(distinct,count,group)

三世轮回 提交于 2019-12-25 21:22:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Mongodb中自带的基本聚合函数有三种:count、distinct和group。下面我们分别来讲述一下这三个基本聚合函数。 (1)count 作用:简单统计集合中符合某种条件的文档数量。 使用方式:db.collection.count(<query>)或者db.collection.find(<query>).count() 参数说明:其中<query>是用于查询的目标条件。如果出了想限定查出来的最大文档数,或者想统计后跳过指定条数的文档,则还需要借助于limit,skip。 举例: db.collection.find(<query>).limit(); db.collection.find(<query>).skip(); (2)distinct 作用:用于对集合中的文档针进行去重处理 使用方式:db,collection.distinct(field,query) 参数说明:field是去重字段,可以是单个的字段名,也可以是嵌套的字段名;query是查询条件,可以为空; 举例: db.collection.distinct("user",{“age":{$gt:28}});//用于查询年龄age大于28岁的不同用户名 除了上面的用法外,还可以使用下面的另外一种方法: db.runCommand({

Showing Distinct Values with Aggregates

走远了吗. 提交于 2019-12-25 18:32:13
问题 I have a table for recording daily price from different suppliers. My goal is to find the best (low price) supplier. The table structure is Table Name: lab1 Columns: ID, Product_ID, Price_date, Price, Supplier ----------------------------------------------------------------------------------- ID Product_ID Price_date Price Supplier -------------------------------------------------------------------------------------- 1 8 26-10-2014 1300 SP1 2 8 05-10-2014 1600 SP2 3 8 15-10-2014 1300 SP1 4 8

How to express count(distinct) with subquery in MySQL?

左心房为你撑大大i 提交于 2019-12-25 15:50:54
问题 A query results a certain number. The query is: select count(distinct case when (A or B or C) and D then table_a.field1 else null end) from table_a left join table_b on table_b.x = table_a.y group by table_a.y ; where A, B, C and D are given conditions. Now, written in this form: select sum((select count(1) from table_b where table_b.x = table_a.y and ((A or B or C) and D) )) from table_a left join table_b on table_b.x = table_a.y group by table_a.y ; the result does not match the one we got

致sql初学者

房东的猫 提交于 2019-12-25 11:53:19
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 很多程序员视 SQL 为洪水猛兽。SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言、面向对象的程序语言、甚至是函数语言(尽管有些人认为 SQL 语言也是一种函数式语言)。 1、 SQL 是一种声明式语言 首先要把这个概念记在脑中:“声明”。 SQL 语言是为计算机声明了一个你想从原始数据中获得什么样的结果的一个范例,而不是告诉计算机如何能够得到结果。这是不是很棒? (译者注:简单地说,SQL 语言声明的是结果集的属性,计算机会根据 SQL 所声明的内容来从数据库中挑选出符合声明的数据,而不是像传统编程思维去指示计算机如何操作。) SELECT first_name, last_name FROM employees WHERE salary > 100000 上面的例子很容易理解,我们不关心这些雇员记录从哪里来,我们所需要的只是那些高薪者的数据(译者注: salary>100000 )。 我们从哪儿学习到这些? 如果 SQL 语言这么简单,那么是什么让人们“闻 SQL 色变”?主要的原因是:我们潜意识中的是按照命令式编程的思维方式思考问题的。就好像这样:“电脑,先执行这一步,再执行那一步,但是在那之前先检查一下是否满足条件 A 和条件 B ”。例如,用变量传参、使用循环语句、迭代

MySQL实用基本操作

淺唱寂寞╮ 提交于 2019-12-25 10:33:41
本博客写是装好Mysql并配好环境变量后的基本操作(windows10系统下)且都是黑框内的操作。 一、登陆MySQL 首先启动服务,在桌面左下角图标处点击右键Windows PowerShell(管理员)(A),然后会出来个蓝框,在蓝框内输入 net start mysql80 (80是对应的MySQL版本如果是其他版本则输入对应的版本号,如5.7版本输入net start mysql57),然后按回车键,若显示服务已启动,则服务启动成功。 在搜索框内输入cmd则会弹出命令提示符点击进去,所谓的黑框就弹了出来,黑框不一定是黑的,可以通过右键点击左上角改变其属性,接着在黑眶内输入 mysql -u root -p ,按回车键,输入你的密码,敲回车,登陆成功进入MYSQL。 二、对数据库的操作 (1)查看数据库: show databases; (2)创建数据库: create database 数据库名; (3)使用数据库: use 数据库名; (4)删除数据库: drop database 数据库名; 部分实例 三、对表的操作 (1)创建数据表 : create table 数据表名 ( 属性1 类型, 属性2 类型,); 类型后边可以加限定条件:primary key(主键)、not null(不能为空)、unique(唯一的)、foreign key(外键)、default

Python Pandas: Group by and count distinct value over all columns?

放肆的年华 提交于 2019-12-25 08:34:49
问题 I have df column1 column2 column3 column4 0 name True True NaN 1 name NaN True NaN 2 name1 NaN True True 3 name1 True True True and I would like to Group by and count distinct value over all columnsI am trying : df.groupby('column1').nunique() but I am receiving this error. AttributeError: 'DataFrameGroupBy' object has no attribute 'nunique' Anybody have a suggestion? 回答1: You can use stack for Series and then Series.groupby with SeriesGroupBy.nunique: df1 = df.set_index('column1').stack()

Display distinct record for each employee

北城余情 提交于 2019-12-25 07:58:18
问题 I have two tables appointment app and faculty fac. they are multiple record with same emp ids I need to join two tables by app.employeeid=fac.empid (here i will get duplicate records) and app.begindate<=now and app.enddate>=now and app.percent>.50 (here also i will get duplicate records because empid(multiple records with same id) can have same begindate,end date and >.50 ) max(app.amt) (here also i will get duplicate records because there can be a chance where empid(multiple records with