mysql帮助文档学习

select * from t1, t2 where t1.id = t2.id;

mysql -h host -u user -p

quit

select version(), current_date;

select sin(pi()/4), (4+1)/5;

select version();

select now();

select user(), current_date;

\c(取消前面输入的)

select * from my_table where name = 'Smith' and age < 30;

show databases;

use test(use 和quit不用分号)

grant all on menagerie .* to 'your_mysql_name'@'your_client_host'; (不解)

create datebase menagerie;

use menagerie

mysql -h host -u user -p menagerie

show tables;

create table pet (name varchar(20), owner varchar(20),

species varchar(20), sex char(1), birth date, death date);

show tables;

describe pet;

load data local infile '/path/pet.txt' into table pet;

insert into pet values ('puff', 'Diane','hamster','f','1990-12-11',NULL);

select what from table where conditions;

select * from pet;

delete from pet;

load data local infile 'pet.txt' into table pet;

update pet set birth = '1998-12-11' where name = 'Bowser';

select * from pet where species = 'snake' or species = 'bird';

select * from pet where (species = 'cat' and sex = 'm') or (species = 'dog' and sex = 'f');

select name, birth from pet;

select distinct owner from pet;

select name, species, birth from pet where species ='dog' or species = 'cat';

select name from pet order by birth desc;

select name, species, birth from pet order by species, birth desc;(desc只影响birth,不影响species)

select name, curdate(),(year(curdate())-year(birth))-(right(curdate(),5)<right(birth,5)) as age from pet;

select ... order by name;

select name, death, (year(curdate())-year(death))-(right(curdate(),5)-right(birth,5)) as age

from pet where death is not null order by age; (注意是is not null)

select name , birth from pet where month(birth) = 5;

select name, birth from pet where month(birth) = month(date_add(curdate(), interval 1 month)); //!!!

select name, birth from pet where month(birth) = mod(month(curdate()), 12) +1;

select 1 is null, 1 is not null;(不能使用1=null, 1<null, 1>null, 1<>null)

select * from pet where name like 'b%';

select * from pet where name like '%fy';

select * from pet where name like '%w%';

select * from pet where name like '_____'; (正好包含5个字符)

select * from pet where name regexp '^b';

select * from pet where name regexp binary '^b';

select * from pet where name regexp 'fy$';

select * from pet where name regexp 'w';

select * from pet where name regexp '^.....$';

select * from pet where name regexp '^.{5}$';

select count(*) from pet;

select owner, count(*) from pet group by owner; (group by!!!)

select species, sex , count(*) from pet group by species, sex;

select species,sex,count(*) from pet where species='dog' or species='cat' group by species,sex;

select species,sex,count(*) from pet where sex is not null group by species, sex;

create table event (name varchar(20), date date, type varchar(15),remark varchar(255));

load data local infile 'event.txt' into table event;

select pet.name, (year(date)-year(birth))-(right(date,5)<right(birth,5)) as age, remark

from pet, event where pet.name = event.name and event.type = 'litter';

select p1.name, p1.sex, p2.name, p2.sex, p1.species from pet as p1, pet as p2

where p1.species = p2.species and p1.sex = 'f' and p2.sex = 'm';

select database();

show tables;

describe pet;

show index from table_name;

mysql<batch-file (批处理,直接把命令都写到batch-file)

mysql -h host -u user -p < batch-file

--force(出错,仍然继续执行脚本)

mysql <batch-file | more (顺便查看输出)

mysql <batch-file > mysql.out (cron定时执行任务时,使用批处理)

mysql -t (批处理时,输出的内容格式与命令行时一样,否则为精简模式)

mysql -vvv(回显被执行的命令)

mysql> source filename; (已经进入mysql命令行时,这样用)

mysql> \. filename;

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.45),(1,'b',3.99);

select max(article) as article 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 article, dealer, price from shop s1 where price

=(select max(s2.price) from shop s2 where s1.article = s2.article); (真复杂)

select @min_price:=min(price), @max_price:=max(price) from shop;(临时变量)

select * from shop where price=@min_price or price=@max_price;

references tbl_name(col_name)

create table person (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 person(id),

primary key (id) );

insert into person values (null, 'autonio paz');

select @last := last_insert_id();

insert into shirt values (null, 'polo', 'blue', @last),(null,'dress','white',@last),

(null,'t-shirt','blue',@last);

insert into person values (null,'lilliana angelovska');

select @last := last_insert_id();

insert into shirt values (null, 'dress', 'orange', @last),(null, 'polo', 'red', @last),

(null, 'dress', 'blue', @last),(null, 't-shirt','white', @last);

select * from person;

select * from shirt;

select s.* from person p, shirt s where p.name like 'Lilliana%' and s.owner=p.id and s.color<>'white';

show create table shirt\G

describe shirt;

select field1_index, field2_index from test_table where field1_index='1' or field2_index='1';

select field1_index, field2_index from test_table where field1_index = '1' union select

field1_index, field2_index from test_table where field2_index = '1';

create table t1 (year year(4), month int(2) unsigned zerofill, day int(2) unsigned zerofill);

insert into t1 values(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2)...;

select year, month,bit_count(bit_or(1<<day)) as days from t1 group by year, month;

create table animals(id mediumint not null auto_increment, name char(30) not null, primary key (id));

insert into animals (name) values ('dog'),('cat');

select * from animals;

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');

select * from animals order by grp,id;

alter table tbl auto_increment=100;

(3.7孪生兄弟。太牛逼了。。几十行的sql语句)

load data infile '/local/access_log' into table tbl_name

fields terminated by ',' optionally enclosed by '"' escaped by '\\'

mysqld mysqld_safe mysql.server mysqld_multi mysql_install_db

mysql mysqladmin mysqlcheck mysqldump mysqlhotcopy mysqlimport mysqlshow

myisamchk myisampack mysqlbinlog perror

mysql test

mysqladmin extended-status variables

mysqlshow --help

mysqldump --user=root personnel

-h localhost | --host==localhost

mysql -u root -p -e "select user, host from user" mysql





你可能感兴趣的:(mysql,学习,文档)