mysql常用代码

来源:互联网 发布:csdn黑马程序员 编辑:程序博客网 时间:2024/06/12 21:35

http://tool.oschina.net/uploads/apidocs/mysql-5.1-zh/sql-syntax.html#sqlps
语法

–查询兴趣是books
select my_contacts.*,interests.interest from my_contacts,contact_interest,interests
where interests.interest=’books’ and
contact_interest.interest_id=interests.id and
my_contacts.id=contact_interest.contact_id;
–另外一种写法
select my_contacts.id,my_contacts.phone,my_contacts.last_name,my_contacts.first_name,my_contacts.email,my_contacts.birthday,my_contacts.prof_id,my_contacts.status_id,my_contacts.zip_id,interests.interest,profession.profession,status.status,zip_code.city,zip_code.state,zip_code.zip_code from my_contacts
inner join contact_interest
on my_contacts.id=contact_interest.contact_id
inner join interests
on contact_interest.interest_id=interests.id
inner join profession
on my_contacts.prof_id=profession.id
inner join status
on my_contacts.status_id=status.id
inner join zip_code
on my_contacts.zip_id=zip_code.id
where interests.interest=’books’;
–创建personbooks表,作为Php查询的视图表。但是表不能一起更新呀?可以插入?怎样插入呀?
–更新,直接通过查询可以更新吗?注意语句方法,不通用。可以呀,就是视图呀。就是查询的代码复制创建的。
create view person_books as
select my_contacts.id,my_contacts.phone,my_contacts.last_name,my_contacts.first_name,my_contacts.email,my_contacts.birthday,my_contacts.prof_id,my_contacts.status_id,my_contacts.zip_id,interests.interest,profession.profession,status.status,zip_code.city,zip_code.state,zip_code.zip_code from my_contacts
inner join contact_interest
on my_contacts.id=contact_interest.contact_id
inner join interests
on contact_interest.interest_id=interests.id
inner join profession
on my_contacts.prof_id=profession.id
inner join status
on my_contacts.status_id=status.id
inner join zip_code
on my_contacts.zip_id=zip_code.id
where interests.interest=’books’;

—–笑。

mysql:
update t1,t2
set t2.nickname=t1.nickname,t1.playNum=t2.playNum
where t1.id=t2.id;
http://blog.itpub.net/29254281/viewspace-1189307/
不同通过子查询更新好烦。
oracl:

update t1 set (nickname,playNum)=(select nickname,playNum from t2 where t1.id=t2.id);

子查询更新

update (    select t1.id t1id ,t1.nickname t1nickname,t1.playNum t1playnum,t2.id t2id ,t2.nickname t2nickname,t2.playNum t2playnum    from t1 inner join t2 on (t1.id=t2.id))set t1.nickname=t2.nickname,t1.playnum=t2.playnum;

内连视图更新

–查询兴趣是music
select * from my_contacts,contact_interest,interests
where interests.interest=’music’ and
contact_interest.interest_id=interests.id and
my_contacts.contact_id=contact_interest.contact_id;

–查询兴趣是books ,music 中的一个
select * from my_contacts,contact_interest,interests
where interests.interest in (‘music’,’books’) and
contact_interest.interest_id=interests.id and
my_contacts.contact_id=contact_interest.contact_id;

–查询兴趣是books,pets 中的一个
select * from my_contacts,contact_interest,interests
where interests.interest in (‘pets’,’books’) and
contact_interest.interest_id=interests.id and
my_contacts.contact_id=contact_interest.contact_id;

–查询所有的人的兴趣的资料
select * from my_contacts,contact_interest,interests
where contact_interest.interest_id=interests.id
and my_contacts.contact_id=contact_interest.contact_id;

–查询兴趣是books,或者music
select * from my_contacts,contact_interest,interests
where contact_interest.interest_id=interests.id
and my_contacts.contact_id=contact_interest.contact_id
and (interest=’books’or interest=’music’) order by my_contacts.contact_id;

–分组统计不同status的人,求和
select status_id,status.status,count(status_id) as countid from my_contacts
inner join status on my_contacts.status_id=status.id
group by status_id;
group by 在连接中是可以用的哦

–分割字符串中字段
update test
set interest4=substring_index(interests,’,’,1);

–截取已经被分割走的字段
update test
set interests=substr(interests,length(interest1)+2);

–删除对应的一行
delete from test
where interests=’first’;

–通过查询创建表的格式,以及其中的数据
因为查询也是有缓存的,包含列名,和数据
create table testb as
select interest from

–创建自己的格式,也通过查询来引入数据,前提,需要列名一样
create table testd(
id int not null auto_increment primary key,
interests varchar(20)
)as
select interests as bieming from testb;

–多个表格的链接
select * from contact_interest,interests
where contact_interest.interest_id=interests.id;

–表的内连接的2种方法,2个表的列不会变

1.select * from contact_interest
inner join interests
on contact_interest.interest_id=interests.id

2.select * from contact_interest,interests
where contact_interest.interest_id=interests.id;

—内连接,选择需要的列
select my_contacts.email,profession.profession from my_contacts
-> inner join profession
-> on my_contacts.prof_id=profession.id;

—内连接的自然连接,就是不用On,默认的列是相同的
select * from my_contacts
nature join interests;
假设他们关联的列是同名的,虽然在现实中不相同,这里仅仅是举例。

—删除key
alter table job_desired
drop key contact_id;

–添加key
alter table job_desired
add key (contact_id);

–添加外箭(必须先创建key)
alter table job_desired
add foreign key (contact_id) references my_contacts(contact_id);

–删除外检(主见也可以被删除,可以修改)
alter table job_desired
drop foreign key job_desired_ibfk_1;

–利用子查询完成表的链接
select contact_id,(select state from zip_code where my_contacts.zip_code=zip_code) from my_contacts;

–查询比2工资大的人,用子查询
select * from my_contacts natural join job_current
where job_current.salary>(
select salary from job_current where contact_id=2);

用subquery子查询,内层先执行
select * from my_contacts natural join job_current
where salary >(
select job_current.salary
from my_contacts natural join job_current
where email=’linda@qq.com’);

–查询邮件是什么的人的信息,用自然连接

select * from my_contacts natural join job_current
where my_contacts.email=’linda@qq.com’;

– 外层查询先执行的关联查询
select * from my_contacts where 2=(
select count(*) from contact_interest
where contact_id=my_contacts.contact_id);

noncorrelated subquery 非关联子查询
–outter 外查询先执行的correlated subquery关联查询,not exists的不存在就满足外层条件。
select * from my_contacts
where not exists
(select * from job_current where my_contacts.contact_id=job_current.contact_id);

—查询wdesired设计师的人员并创建视图
create view web_designers as
select mc.first_name,mc.last_name,mc.phone,mc.email
from my_contacts mc
natural join job_desired jd
where jd.title=’wdesigner’;
—查看视图就像是一张表那样查看select * from web_designers
+————+———–+———–+—————————–+
| first_name | last_name | phone | email |
+————+———–+———–+—————————–+
| zhou | mu | 555343434 | mm@yahoo.com |
| Go | Linda | 555234123 | linda@qq.com |
| Nigel | Moore | 555231111 | nigelmoore@ranchersrule.com |
| Go | Linda | 555234123 | linda@qq.com |

+————+———–+———–+—————————–+

information_schema.columns 所有列
information_schema.KEY_COLUMN_USAGE 外键及其关联表
一般主见用id作为名,方便删除,修改。

—-2017/9/17——-

把文档看一遍真的是不错的选着,很多疑问,或许都可以解答了。关于语句的用法,每个数据库千差万别。

1.【】可选的部分
2.|或者
3.{}必须选择的部分
4./**/注释,可以加载中间
5.select 官方文档http://tool.oschina.net/uploads/apidocs/mysql-5.1-zh/sql-syntax.html#select

其实select几个表,或者一个表,都是一种单链表的获取,插入。
为甚是获取呢?因为如果一个列名存在其中,那就是获取
为甚是插入呢?如果一个列名不在其中,就插入到链表中,可以随意安排插入位置哦。就是select 后面的位置排序说明了插入的地方。你想,一个链表已经把模式进行了插入,那里面的值肯定有要跟着插入过去啊。如果是什么都不规定,那就是笛卡尔积,也就是一个左表的一行,对应以一个右表,因为每一行都要执行select语句,根据模式嘛,为甚是叫模式呢。因为它的行都要遵守呀。

但是要注意了,select出来的是一个虚假的表,并未在原有的表中进行更改,所以,如果你想真的改表,那还是要alter哦。把select看做是链条的concat吧,模式的concat。

上厕所的时候让我想到了,左外链接就是除了交集还要还要加上左表有右表没有,同理右,自然连接就是左右都有,有交集的行。

每个表都有模式,那就是表和其他表的关系,怎样执行求链表的交集,并集,差集,左差集,右差级,就是c语言的数据结构中才联系的那个函数。对每一个行都执行这样的函数。所以,连接就是求每一行的交集,并集,有关系的一种运算。

—-统计有兴趣爱好的人,以及他们的爱好总数
select my_contacts.,count()num from my_contacts join contact_interest on my_contacts.id=contact_interest.contact_id group by my_contacts.id;

—–列出有兴趣爱好的人的信息,包括没有爱好的人
select * from my_contacts left join contact_interest on my_contacts.id=contact_interest.contact_id;
–在group by中使用having,limit
select color, toy_id,max(toy_id) from color group by color limit 2,5;

掌握语法,真的比较重要,语法是数据结构和英语的结合。

select作为一个表,可以写入information系列的表中,记录其关系,主键,如果仅仅是select的一个虚表,根本就不可能把它的信息记录很详细,这就是看你需要什么了,合理使用。只要是真正的表就有模式,那就有记录模式,记录具体数据的系统表。

—–2017/09/19—–
is null是结果数据为null,但是实际的表中数据可能不是这样,如果查询中变为null,那也要注意哦。
优化数据库是一个很好的赚钱方式。
子查询可以想象是没有连接查询前,一个大的问题,我们需要一步步通过简单查询来解决。
—-查询出人的state,通过子查询新建一个列,是来自其它表的查询结果
select mc.first_name,
(select state
from zip_code
where mc.zip_id=id)as state
from my_contacts mc;

—也可用连接,时间更短的.join
select testuser.*,testlingshi.id from testuser right join testlingshi using(id) where testuser.id is null;

如果一个字符串和一个字符串concat,我们肯定要有左右之分,就是谁作为循环
的开头。如果一个是从左中的每一个循环,那就是可以找到不在右表中的数值,
那如果是从右的每一个循环,就能找到不在左表中的数值。这就是左右连接的本质。

—把左连接和外链接union在一起呢
select testuser.*,testlingshi.id from testuser right join testlingshi using(id) where testuser.id is null
-> union all
-> select testuser.*,testlingshi.id from testuser left join testlingshi using(id);

—把左表中交集和差集都显示出来,但是,这个表不会像left join那样链接
只是一个列的数据,但是我们可以加上连接。也就是不能返回多列。

intersect,minus,不能支持,但是可以用其他替换

select id,mobile from testuser
where testuser.id not in
(select id from testlingshi)
union
select id,mobile from testuser
where testuser.id in
(select id from testlingshi)
inner join testlingshi using(id);

子查询和连接,他们就是一次执行,和一次执行多次的不同操作。但是连接为什么
要快呢?因为什么呢?
—-查询在user中的又在testlingshi中的id。

MariaDB [test]> select id,mobile from testuser where id in
-> (select id from testlingshi);

—加上testlingshi的链家以后呢?
select testlingshi.id from testlingshi,
(select id from testuser
where id in (select id from testlingshi)
)as a
where testlingshi.id=a.id;
结果就是我们想要的left join连接
select id from testlingshi left join testuser using(id) where testuser.id is not null;

所以殊途同归呀。但是你也愿意用简单的代码不是吗?

in not in ////不仅仅是判断,还有结果—和left,right join有非常多的雷同,区别也就是交集是否存在。所以那个优化问题,其实本身就是这判断,但是left join更一气合成吧。
exists not exists////布尔判断

select id from testuser where id not in (select id from testlingshi);

—-约束内容constraint check在mysql中不存在哦。
add constraint aaa check sex in (‘woman ‘,’man’);

—-视图view到底,多了什么功能呢?比table?
create view x_sex_view as
select * from my_contacts where sex<>’woman’ and sex<>’man’;

create view x_sex_table2 as
select * from my_contacts where sex<>’woman’ and sex<>’man’;
在view中的视图数据是动态增加的,也就是说,他其实存储了一个查询,当我哦们查询的时候,他就再调用查询。
而table呢?就是一个表,他不会存储的命令,而是数据。是不变了哦。

—-创建一个视图
create view qb_quarters as
-> select * from piggy_bank where coin=’Q’;
—-创建一个视图
create view qb_dimes as
select * from piggy_bank where coin=’D’ with check option;

—-插入一个数据到视图,也就是按照视图创建的select的where,from,来判断条件,插入原始表。
where:coin=’D’
from:piggy_bank;
insert into qb_dimes values(”,’D’,2005);=====
insert into piggy_bank values(”,’D’,2005);实际的操作。

—不是说没有check嘛?那可不可以在视图中利用一个check option来模仿check constraint。。呵呵呵呵
笑开森。

—-删除视图drop view;

—-start transaction
—–rollback
—–commit;

事务就是一个封闭的函数哦。执行的时候,是独占资源的。

——一个小丑的活动
select * from info_activities
-> inner join activities using(activity_id)
-> inner join clown_info using(id);
—–一个小丑的地点
select * from info_location
inner join location using(location_id)
inner join clown_info using(id);

—-一个权限表( )

—–一个用户表 ( USER_STATISTICS,kong,)

—–一个权限用户表(USER_PRIVILEGES)

—–一个角色表(APPLICABLE_ROLES,kong )

—–一个权限角色表

—–一个角色用户表

呵呵呵,在mysql内部,有一个很大的数据库哦,是自己带的,可以用来为用户管理进程提供关系数据。这也就是小小的数据库哦。。有关系哦。。有1对多哦。。。

所以,慢慢的了解information.schema这个数据库,会发现很多关系,很多有用的为程序需求设计的哦。。。所以mysql 不仅仅是个表,而且是个应用数据库的程序哦。

原创粉丝点击