---
title: "sql查询"
author: "sinxadmin"
date: "2011-04-12T13:52:36Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2011-04-13T11:04:43Z"
id: 998848
url: "https://sinxadmin.iteye.com/blog/998848"
---

1.查询重复数据

查询test表中A列值为100的时候B列重复的数据,例如：

A        B       C        D

100    hz

100    lh

100    hz

那么显示B为hz的行

select * from test where A= 100 and  B in (

select B from test where A= 100 group by A,B  having count(\*) &gt; 1 )

2.分组去重复统计查询

统计test表中每天的用户访问量，例如：

A                      B      C        D

2011-01-01      a

2011-01-01      b

2011-01-01      a

2011-01-02      b

2011-01-02      b

显示结果为

A                      B

2011-01-01      2

2011-01-02      2

 

select A ,count(\*) from  (select  * from test   group by A,B) test group by A

先通过select  * from test   group by A,B去重复再进行分组计算

 

或者可以使用select count(dinstinct B) from test group by A

 

3.mysql设置列唯一性

create unique index column\_index on  table(column)

 

4.mysql insert忽略错误

insert ignore into tb(...) value(...)

 

5.mysql查看表创建语句

mysql&gt; show create table  tb

 

6.mysql查看列的详细信息

mysql&gt; show full columns from tb

 

7.mysql查看字符集

mysql&gt; show variables like '%char%'

 

8.mysql设置连接字符集

set names gb2312;