create table pet ( name varchar(20), ower VARCHAR(20), species varchar(20), sex char(1), birth DATE, death DATE ) desc pet INSERT into pet VALUES('Fluffy','Harold','cat','f','1993-02-04',NULL)
INSERT into pet VALUES('Claws','Gwen','cat','m','1994-03-17',NULL) INSERT into pet values ('Buffy','Harold','dog','f','1989-05-13',NULL) INSERT into pet values ('Fang','Benny','dog','m','1990-08-27',NULL) INSERT into pet values ('Bowser','Diane','dog','m','1979-08-31','1995-07-29') insert into pet values ('Chirpy','Gwen','bird','f','1998-09-11',NULL) INSERT into pet values('Whistler','Gwen','bird','','1997-12-09',NULL) insert into pet values ('Slim','Benny','snake','m','1996-04-29',null) select * from pet
SELECT name,birth,CURDATE(), (year(curdate()-year(brith))) from pet
select name,birth,month(birth)as ‘月份‘ from pet where month(birth)=5
select 1=NULL,1<>null,1<null,1>NULL/*z这些都是无意义的结果*/ SELECT 1 is NULL,1 is not null /*在MYSQL中,0或者NULL意味着假其他意味着真*/ select 0 is null,0 is not null,'' is null,'' is not null/*这条语句说明了一个问题*/
select * from pet where name like '%FY' select * from pet where name like '.....' SELECT * FROM pet where name REGEXP BINARY '^B' /*区分首字母大小写使用 binary 使一个字符串变成二进制字符串*/ select * from pet where name REGEXP 'fy$'/*查询以FY结尾的姓名*/ select * from pet where name REGEXP 'w'/*查询一个包含’w‘的名字需要注意中英文的切换*/ select * from pet where name REGEXP '^.....$'/*找出正好包含五个字符的名字*/ select * from pet where name REGEXP '^.{5}$' /*重复n次{n},等同于上面这个语法*/
select ower,count(*) from pet group by ower /*每个业主养了多少宠物*/ select species ,count(*) from pet group by species/*宠物的分类*/
select sex ,count(*) from pet group by sex/*宠物的性别分类*/ select species,sex,count(*) from pet where species ='dog' or species='cat' group by species,sex /*查询狗喝猫的数目和性别*/ ========================= create table event( name varchar(20), date DATE, type varchar(15), remark varchar(255)
) desc event / insert into event values ('Buffy','1993-06-23','litter','5 puppies, 2 female,3 male') INSERT into event values ('Buffy','1994-06-19','litter','3 puppies,3 female') INSERT into event values ('Chirpy','1999-03-21','vet','needed beak straightend') INSERT into event values ('Slim','1997-08-03','vet','broken rib') INSERT into event values ('Bowser','1991-10-12','kennel',NULL) insert into event values ('Fang','1991-10-12','kennel',NULL) INSERT into event values ('Fang','1998-08-28','birthday','Gave him a new chew toy') INSERT into event VALUES ('Claw','1998-03-17','birthday','Gave him a new flea collar') INSERT into event values ('Whistler','1998-12-09','birthday','Frist brithday') */ SELECT * FROM event select DATABASE()/*查询当前选择的数据库*/ show tables/*查询当前操作的表*/ select DISTINCT(species) from pet /*查询宠物种类*/
use test create table shop ( -> article INT(4) UNSIGNED zerofill default '0000' not null, -> dealer CHAR(20) default '' not null, -> price DOUBLE(16,2) DEFAULT '0.00' NOT NULL, -> PRIMARY KEY (article,dealer));
create table shop ( article int(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL, dealer char(20) DEFAULT '' not null, price DOUBLE(16,2) DEFAULT '0.00' not null, PRIMARY KEY(article,dealer)) ) INSERT into shop values (1,'A',3.25),(2,'A',10.99),(3,'B',1.45),(3,'C',1.69),(3,'D',1.25),(4,'D',19.95) select * from shop
select article,dealer,price from shop where price=(select max(price) from shop) /*找出最贵物品的编号、销售商的价格*/ select article,dealer,price from shop order by price desc limit 1 /*另一个思路,找出最贵物品的编号、销售商的价格*/ SELECT article,max(price) as price from shop group by article /*查询每项物品的最高价格*/ select * from shop select @min_price:MIN(price),@max(price):max(price) from shop /*使用用户变量*/
create table persion( id SMALLINT UNSIGNED not null auto_increment, name char(60) not null, PRIMARY key(id) );
create table shirt( id SMALLINT UNSIGNED not null auto_increment, style enum('t-shirt','polo','dress') not null, color enum('red','blue','orange','white','black') not NULL, owner SMALLINT UNSIGNED not null REFERENCES persion(id), PRIMARY KEY(id) )
select * from shirt create table animals( grp enum('fish','mammal','bird') not null, id MEDIUMINT not null auto_increment, name char(30) not null, primary key(grp,id) )
INSERT into animals (grp,name)VALUES ('mammal','dog'),('mammal','cat'), ('bird','penguin'),('fish','lax'), ('mammal','whale'),('bird','ostrich') SELECT * from animals ORDER BY grp,id; alter table animals auto_increment=100
select * from animals
mysqlshow
[root@www bin]# mysql -u root -p -e "select user,host from user" mysql Enter password: +------+-----------------------+ | user | host | +------+-----------------------+ | root | % | | root | 127.0.0.1 | | | localhost | | root | localhost | | | localhost.localdomain | | root | localhost.localdomain | +------+-----------------------+ /*对mysql偶尔有用的另一个选项是-e或者--execute选项,可以将sql语句传递给服务器*/