mysql_控制台基本使用

来源:互联网 发布:c语言标准库函数 下载 编辑:程序博客网 时间:2024/04/28 13:59

--  登录这部分

sql> mysql -u sa - p

sql>123 


-- 基本表、数据库的命令使用

sql> show basetables; // 有一个s, 不区分场景

+----------------------------------------+

| Database |

+----------------------------------------+

| information |

| lw_db    |

| mysql |

| scujcc |

+----------------------------------------+


sql> use scujcc;

sql> show tables;

+----------------------------------------+

| t_bank |

...

+----------------------------------------+

sql> describe t_bank;

-- 可以看到表结构

sql> select * from t_bank;

-- 可以看到表里所有的数据


-- 基本操作语句的使用

1.查询

基本的统计表单

select * from

(select count(*), if(sum(amt) = NULL, 0, sum(amt)) sum from t_order_info)  a //一定要有别名

... b


使用嵌套查询而不是left join, 统计交了此项项目的人员

select (select count(*) from t_order_info where feeitem_id = o.feeitem_id and order_status = 1) 

count_person from t_feeitem o // count只能直接作用于数据, 而不能嵌套查询


使用count而不是in或者not in, 统计未支付的人员数量

select (

select count(*) from t_customer_fee_list cust //订单表里的数量非常大, 不能使用in或者not in

where cust.feeitem_id = t.feeitem_id

and (select count(*) from t_order_info

where cust_no = cust.cust_no

and order_status = 1) = 0

) not_pay_num from t_feeitem t;


union 相同主键的数据则融合起来

union all 相同主键的都存在


修改表结构

drop table if exists `t_test_info`;

create table `t_test_info` (

`id` int not null auto_increment comment '编号',

`login_id` varchar(32) not null comment '用户名',

`password` varchar(32) not null comment '密码',

`create_date` date not null default date_format(now(), '%Y-%m-%d' ) comment '创建时间',

`create_time` time not null default date_format(now(), '%H:%i:%s') comment '创建详细时间',

`create_timestamp` datetime not null default date_format(now(), '%Y-%m-%d %H-%i-%s') comment '详细时间',

`remark` varchar(256) not null comment '备注',


primary key (`id`),

unique key (`login_id`) //不能用逗号

) comment '测试表';


alter table `t_order_info` add column `remark` varchar(256) null comment '备注' after `sync_flag`;

alter table `t_order_info` drop column `remark`;


delete from `t_order_info` where id = 1123;

drop table if exists `t_order_info`;

drop database if exists `hello`;

insert, update, delete用于记录, drop用于对象基本类似


0 0
原创粉丝点击