mysql数据库查询笔记_mysql数据库笔记

虽然select语句可以一次性查出所有的数据,但受计算机屏幕大小的限制,每次只能看到部分行的内容,要看其它内容的时候就要用到滚动条。由于网络的限制,对于web应用来说,这种方式的效率非常低下,数据量比较大的时候几乎是不能使用的。

事实上,通常采用的方法是根据屏幕的大小,一次只查出部分行并显示,如果要看其它的内容,通常采用分页的方式。

那如何在SQL语句中查部分行的内容呢?就要用到limit关键字了。

kingbase中可以用下面的语句:

1、查询第一行记录:

select *

from 表名 limit 1

2、查询第n行到第m行记录

select * from 表名 offset

n-1 limit m-n;

3、查询前n行记录

select * from 表名 limit n;

mysql中可以用下面的语句:

1、查询第一行记录:

select *

from 表名 limit 1

2、查询第n行到第m行记录

select * from 表名 limit

n-1,m-n;

3、查询前n行记录

select * from 表名 limit n;

上面的n和m可以通过计算得到,比如用“select count(*) from

表名”先得到所有的数据的个数,再根据每一个页面能够显示的项目数,进行简单的计算,就可以得到想要得结果。

灵活运用上面的语句有时可以起到意想不到的效果!比如要查找选修课程最多的学生的学号和选课数可以只用下面的一条语句就可以实现了。

select

学号,count(课程号) 选课数 from 学生选课.选课 group by 学号 order

by 选课数 desc limit 1;

当然如果只想查出选修课程最多的学生的学号就要麻烦一些了,用下面的语句

select 学号

from(select 学号,count(课程号) 选课数 from 学生选课.选课 group

by 学号) t order by t.选课数 desc limit 1;

创建表

create tabel 表名

create table customers

(

cust_id    int        not null auto_increment,

--不能为空,增量(可以付初始值))

cust_name    char(50)    not null,

cust_address    char(50)    null,

cust_city    char(50)    null,

cust_state    char(5)        null,

cust_zip    char(10)    null,

cust_country    char(50)    null,

cust_contact    char(50)    null,

cust_email    char(50)    null,

primary key(cust_id)

--指定主键

)engine=innodb;

--引擎

组合主键

primary key(order_num,order_item)

--删除数据库

drop database 数据库名字

------------------------------------------------------------

create table customers

(

item_price decimal(8,2)    not null,

cust_name    char(50)    not null,

quantity    int        not null default 1,

cust_address    char(50)    null,

cust_city    char(50)    null,

cust_state    char(5)        null,

cust_zip    char(10)    null,

cust_country    char(50)    null,

cust_contact    char(50)    null,

cust_email    char(50)    null,

primary key(cust_id,quantity)

--指定主键

)engine=innodb;

------------------------------------------------------------------

--给vendors表增加一个vend_phone的列表

alter table vendors

add vend_phone char(20);

--删除整个表

drop table customers2;

--重命名表

rename table customers2 to customers;

--删除列

alter table vendors

drop column vend_phone;

只修改列的数据类型的方法:

通常可以写成 alter table 表名 modify column 列名 新的列的类型

例如:student表中列sname的类型是char(20),现在要修改为varchar(20),SQL语句如下

alter table student modify column sname varchar(20);

同时修改列名和列的数据类型的方法:

通常可以写成 alter table 表名 change column 旧列名 新列名 新的列类型

例如:student表中列sname的类型是char(20),现在要修改为stuname

varchar(20),SQL语句如下

alter table student change column sname stuname

varchar(20);

----------------------------------------------------------------------------------------------------

--插入行

insert into customers

(

cust_name,

cust_address,

cust_city,

cust_state,

cust_zip,

cust_country,

cust_contact,

cust_email

)

values

(

null,

‘pep e.lapew‘,

‘100 main street‘,

‘los angeles‘,

‘ca‘,

‘90046‘,

‘usa‘,

null,

null,

);

--插入多行

insert into customers

(

cust_address,

cust_city,

cust_state,

cust_zip,

cust_country

)

values

(

‘pep e,lapew‘,

‘100 main street‘,

‘los angeles‘,

‘ca‘,

‘90046‘,

‘usa‘

),

values

(

‘m.martian‘,

‘42 galaxy way‘,

‘new york‘,

‘ny‘,

‘11213‘,

‘usa‘

);

--表插入到表

insert into customers

(

cust_id,

cust_contact,

cust_email,

cust_name,

cust_address,

cust_city,

cust_state,

cust_zip,

cust_country

)

select cust_id,

cust_coutact,

cust_email,

cust_name,

cust_address,

cust_city,

cust_state,

cust_zip,

cust_country

--从custnew里面

from custnew;

------------------------------------------------------------------------

修改行

update customers

set     cust_email = ‘elmer@fudd.com‘

cust_name = ‘the fudds’

where     cust_id = 10005;

删除值

update customers

set    cust_email = null

where     cust_id = 10005;

删除一行

delete from customers

where cust_id = 10006;

修改所有行

----------------------------------------------------------------------------

--降低into优先级

insert low_priority into

--降低update优先级

insert low_priority update

--降低delete优先级

insert low_priority delete

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值