一个小白如何创建MYSQL数据表_MySQL小白扫盲(二)--建表、添加、查询
1.SELECT子句
字句名稱 使用目的
select 確定結(jié)果集中應(yīng)該包含哪些列
from ? ?指明所要提取數(shù)據(jù)的表,以及這些表示如何連接的
where 過(guò)濾掉不需要的數(shù)據(jù)
group by 用于對(duì)具有相同列值的行進(jìn)行分組
having 過(guò)濾掉不需要的組
order by ?按一個(gè)或多個(gè)列,對(duì)最后結(jié)構(gòu)集中的行進(jìn)行排序
現(xiàn)在假如我有一個(gè)員工表,主要有4個(gè)字段,id(員工id)、fname(姓)、lname(名字)、work_date(時(shí)間)。
--建表
CREATE?TABLE?`user`?(
`id`?int(11)?unsigned?NOT?NULL?AUTO_INCREMENT?COMMENT?'ID',
`category`?varchar(20)?COLLATE?utf8_turkish_ci?NOT?NULL?COMMENT?'類別',
`parentid`?int(11)?NOT?NULL?DEFAULT?'0'?COMMENT?'上級(jí)',
`work_date`?int(11)?NOT?NULL?COMMENT?'時(shí)間,
PRIMARY?KEY?(`id`),
KEY?`fl`?(`createtime`)
)?ENGINE=InnoDB?AUTO_INCREMENT=46056?DEFAULT?CHARSET=utf8?COLLATE=utf8_turkish_ci;
--添加內(nèi)容
--查詢
(1)如何獲得員工id為99號(hào)的員工的所有信息?
(2)如何獲得id?大于等于20,小于等于40的員工信息?(請(qǐng)用兩種不同方式分別實(shí)現(xiàn))
(3)如何獲得11,45,99,124號(hào)員工的信息?(用兩種方式實(shí)現(xiàn))
(4)如何獲得除了11,45,99,124號(hào)員工外,其他員工的信息?(用兩種方式實(shí)現(xiàn))
(5)如何獲得入職時(shí)間在2011年10月1日前的,并且姓 ‘李’的所有員工?(用三種方式實(shí)現(xiàn))
(6)如何獲取所有emp_id的末尾為1的所有記錄,比如emp_id為1,11,21,31.。。。101,121,。。。1001,1011,。。。。。(用三種方式來(lái)實(shí)現(xiàn))
(7)如何獲取101,111,121,131,141,151,161,171,181,191這幾個(gè)員工的記錄?(分別用通配和正則來(lái)實(shí)現(xiàn))
上面的這些問(wèn)題基本涵蓋了where語(yǔ)句中的所有知識(shí)點(diǎn),大家可以先試試看,按照題目的描述和括號(hào)中的條件來(lái)實(shí)現(xiàn)。
思考后,再查看下面的答案。
答案:
(1)select * from employee where emp_id = 99;
(2)select * from employee where emp_id between 20 and 40;
select * from employee where emp_id >=20 and emp_id <=40;
(3)select * from employee where emp_id = 11 or emp_id = 45 or emp_id = 99 or emp_id = 124;
select * from employee where emp_id in (11,45,99,124);
(4)select * from employee where emp_id !=11 and emp_id !=45 and emp_id != 99 and emp_id !=124;
select * from employee where emp_id not in (11,45,99,124);
(5)select * from employee where start_date
select * from (select * from employee where fname = '李' )? d? where d.start_date < '2011-10-01';
select * from employee where emp_id in (select emp_id from employee where fname = '李' ) and start_date < '2011-10-01';
(6)select * from employee where emp_id like '%1';
select * from employee where emp_id regexp '.*1$';
select * from employee where right(emp_id,1) = 1;
(7)select * from employee where emp_id like '1_1;'
select * from employee where emp_id regexp '1.1';
總結(jié)
以上是生活随笔為你收集整理的一个小白如何创建MYSQL数据表_MySQL小白扫盲(二)--建表、添加、查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华为回应取消充电器 国产厂商千万别跟苹
- 下一篇: linux 临时文件 锁,linux –