create database [databasename];
show databases;
use databasename;
create table sellingpoint(
id int not null primary key,
author varchar(100),
title varchar(200));
desc tablename;
1.删除表
drop table tablename;
truncate table tablename;
alter table tablename add colname varchar(100);
4.删除列
alter table tablename drop columnname;
5.更改列属性
alter table tablename modify columnname varchar(100) ...;
alter table tablename change name1 name2 varchar(100);//后面必须写上列的属性
alter table change name1 name2 varchar(100);
1.插入数据
insert into tablename(col1,col2,col3...) values (value1,value2,value3...)
create
insert into mytable(col1,col2) select col1,col2 from mytable2;
2.删除数据
delete from tablename where ...
update tablename set columename=' ' where ...
select col1,col2 from tablename where ...
select * from sellingpoint where id=10;
select distinct col1,col2 from tablename where ...
select * from tablename where ... limit 3,4; //从查询到的结果的第3行开始,显示4行数据
count函数是用来统计表中或数组中记录的一个函数,
count(*) 它返回检索行的数目, 不论其是否包含 NULL值。
例如:SELECT COUNT(*) FROM sellingpoint;
COUNT(DISTINCT 字段),返回不同的非NULL值数目;若找不到匹配的项,则COUNT(DISTINCT)返回 0 。
select col1,count(*) from sellingpoint where ... group by col1;
select col1,col2 from tablename where ... order by colname desc/asc;
select * from tablename where author like '刘%';
select * from sellingpoint where author like '_永';
select * from sellingpoint where author not like '%永%';
select col1*col2 as alias from selling2;
concat()用来连接字段,用trim清除字段的空格
select concat(trim(author),'+',trim(content)) from selling2 where ...;
把具有相同值的列放在同一组中,默认按分组字段排序,也可以选择按汇总字段排序。
select col1,count(*) as num from selling2 where id>5 group by author order by num;
规定
select author,count(*) as num from selling2
where id>3
group by author
having num>2
order by num ;
子查询中只能返回一个字段的数据。
连接用于连接多个表,使用join关键字,且条件语句用on不用where
可以替换子查询,且效率比子查询快
select c.name,o.id, o.customers_name from customers as c inner join orders as o on c.name=o.customers_name;
自连接可以看成内连接的一种,只是连接的表是自身而已
自然连接是把同名列通过等值测试连接起来的,同名列可以有多个。
内连接和自然连接的区别:内连接提供连接的列,而自然连接自动连接所有同名列。
SELECT A.value, B.value
FROM tablea AS A NATURAL JOIN tableb AS B;
外连接保留了没有关联的那些行。分为左外连接,右外连接以及全外连接,左外连接就是保留左表没有关联的行。