基本sql语句
mysql -h localhost -P 3306 -uroot -pwxf
show databases;
show tables;
desc tblUser;
show create table tblUser;
alter table table_name drop index `uk_name`;
alter table table_name add unique key `new_uk_name` (`col1`,`col2`);
修改表主键自增从10000开始
alter table tblStudent AUTO_INCREMENT=10000;
insert into tblUser (uid,age,user_name,py_name) values
(1111,21,"1019测试1","1019ceshi1"),
(222,21,"李先生","lixiansheng");
update tblOrderInfo set name= REPLACE(name,'白象','方便面') where name like "白象";
alter table tblUser ADD sale_uid bigint(20) unsigned NOT NULL Default 0 COMMENT '销售uid';
delete from tblWxf where uid=2135385251;
alter table tblArticle modify source VARCHAR(10) NOT NULL DEFAULT '' COMMENT '测试修改'
alter table tblArticle change source_old source_new VARCHAR(10) NOT NULL DEFAULT '' COMMENT '测试修改'
alter table tblUser drop grade_id,drop phone
mysql必知必会笔记
select uid tblUser;
select * tblUser;
select count(*) tblUser;
select distinct uid tblUser;
select distinct uid,user_name tblUser;
select * from tblUser order by uid limit 1 offset 2
select * from tblUser order by uid limit 2,1;
select * from tblUser order by grade_id asc,id desc;
select * from tblUser order by 2,3;
select * from tblUser where id<50 and grade_id>0;
select * from tblUser where id<30 or grade_id>=7;
select * from tblUser where id in (122,23,32,567);
select * from tblUser where not id=1;
select * from tblUser where (id>30 or user_name='wuwuwu') and grade_id>=7;
select * from tblUser where grade_id between 5 and 7;
select * from tblUser where grade_id>0 order by uid limit 1 offset 2;
select * from tblUser where grade_id>0 order by updated_at desc limit 1 \G;
select * from tblUser where user_name like '吴%';
select * from tblUser where user_name like '%小芳%';
select * from tblUser where user_name like '吴%01';
select * from tblUser where user_name like '吴_01';
select * from tblUser where py_name like '[wu]%';
select concat(uid,':\t', user_name,"") from tblUser;
select concat(uid,'(', user_name,")") from tblUser;
select concat(uid,'(', user_name,")") as newUidName from tblUser order by newUidName;
select uid,user_name,concat(uid,'(', user_name,")") as newUidName from tblUser;
select 2*3;
select trim('abc');
select curdate();
select py_name,upper(py_name) as py_name_upper from tblUser;
select py_name,lower(py_name) as py_name_lower from tblUser where uid=2135618804;
select py_name,length(py_name) as py_name_length from tblUser;
select uid,length(uid) as uid_length from tblUser;
select * from tblUser where year(created_at)=2022 and month(created_at)=2 and day(created_at)=15;
select pi();
select abs(-5);
select cos(pi());
select avg(grade_id) from tblUser where uid in (2135447236,2135594505,2135508703);
select max(grade_id) from tblUser where uid in (2135447236,2135594505,2135508703);
select min(grade_id) from tblUser where uid in (2135447236,2135594505,2135508703);
select sum(grade_id) from tblUser where uid in (2135447236,2135594505,2135508703);
select count(grade_id) from tblUser where uid in (2135447236,2135594505,2135508703);
select avg(distinct grade_id) from tblUser where uid in (2135447236,2135594505,2135508703);
select grade_id,count(*) from tblUser group by grade_id;
select DATE_FORMAT(create_time,'%H') as hour,count(*) as cnt from tblOrderInfo where id=8 group by hour;
select DATE_FORMAT(create_time,'%H') as hour,count(*) as cnt from tblOrderInfo where id=8 group by hour order by cnt desc;
select DATE_FORMAT(create_time,'%D') as day,count(*) as cnt from tblOrderInfo where id=8 group by day order by cnt desc;
select user_id,count(*) as num from tblOrderInfo group by user_id having num>2 order by num desc;
SELECT id,sales_uid, count(*) AS cnt FROM `tblClue` WHERE `id` = 1456 AND sales_uid in (301028,241008,10100120004,10000060001) GROUP BY `id`,`sales_uid`;
select min(date_day),sku_id from tblSkuRelation where sku_id in (select sku_id from tblSku where sku_type in (1,2) and start_date>20220228 and start_date<20220302) group by sku_id;
select user_name,order_id from tblUser,tblUserAgreement where tblUser.uid=tblUserAgreement.uid;
select user_name,order_id from tblUser inner join tblUserAgreement on tblUser.uid=tblUserAgreement.uid;
select concat(uid,user_name) as uid_name from tblUser where grade_id>0;
select uid,user_name from tblUser as u where u.grade_id>0;
select u2.uid,u2.age from tblUser as u1,tblUser as u2 where u1.uid=2135385251 and u2.age=u1.age;
select agr.*,agrDetail.status from tblUserAgreement as agr,tblUserAgreementDetail as agrDetail where agr.status in (4,5) and agrDetail.date>'2022-02-28' and agr.order_id=agrDetail.order_id and agr.sku_id=agrDetail.sku_id and agr.status!=agrDetail.status;
select tblOrderInfo.user_id,tblOrderInfo.order_id,tblOrderInfo.status,tblUserAgreement.sku_type from tblUserAgreement right outer join tblOrderInfo on tblUserAgreement.uid= tblOrderInfo.user_id and tblUserAgreement.order_id=tblOrderInfo.order_id;
select * from tblUser where uid=236540712 union select * from tblUser where age=4;
select * from tblUser where uid=236540712 or age=4 order by age desc;
select * from tblUser where uid=236540712 union all select * from tblUser where age=4;
insert into tblUser (uid,name,age) values(236540712,'lqc',22);
insert into tblUser (uid,name,age) select uid,name, age from tblUser where uid in (2135385251,2135492756);
create table tblNewUser as select * from tblUser;
create table tblWxf like tblUser;
update tblWxf set id=1234,user_name='cecece' where uid=2135385251;
delete from tblWxf where uid=2135385251;
CREATE TABLE tblWxf1 (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
uid bigint(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户uid',
name varchar(64) NOT NULL DEFAULT '' COMMENT '用户姓名',
grade_id int(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户年级',
PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARSET = utf8mb4 COMMENT '测试表';
CREATE TABLE `tblUser` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL COMMENT '用户姓名',
`sex` varchar(10) NOT NULL COMMENT '性别:男,女',
`age` int(3) NOT NULL COMMENT '年龄',
`pass_word` varchar(64) NOT NULL DEFAULT '0' COMMENT '用户登录密码',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='用户表'
alter table tblWxf add phone varchar(64) not null default '' comment '用户手机号';
alter table tblWxf drop column test;
drop table tblTest;
begin;
update tblWxf set name='测试事务step1' where id=2;
insert into tblWxf (uid,name) values (99999,'测试事务step2');
commit;