Mysql第三章:连接查询和子查询方式,数据操纵

连接查询:    
(1)内连接:inner join

(2)外连接:        左(外)连接  left join 
                        右(外)连接  right join

                        全外连接  full join
(3)交叉连接  (笛卡尔积)

1.查询语句

(1)查询与“张志国”同一班级的学生信息(使用连接查询和子查询方式)。

select student2.sno,student2.sname,student2.ssex,
	student2.s_class,student2.s_birth,student2.s_phone
from student_info as student1,student_info as student2
where student1.s_class=student2.s_class
	and student1.sname='张志国'
	and student2.sname<>'张志国';
select * from student_info
    where s_class=(select s_class
                              from student_info where sname = '张志国')
    and sname <> '张志国';

(2)查询比“计算机应用基础”学时多的课程信息(使用连接查询和子查询方式)。

select course2.cno,course2.cname,course2.ctime
from course_info as course1,course_info as course2
where course1.ctime>course2.ctime
    and course1.cname='计算机应用基础';
select * from course_info
    where ctime>(select ctime
                 from course_info where cname = '计算机应用基础');

(3)查询选修课程号为K002的学生的学号、姓名(使用连接查询、子查询、使用exists关键字的子查询)。

select student_info.sno,student_info.sname
from student_info ,work_info 
where work_info.cno='K002'
      and student_info.sno=work_info.sno;
select sno,sname
from student_info
where (sno in (select sno from work_info
                      where cno='K002'));
select sno,sname
from student_info
where exists (select * from work_info
              where  sno=student_info.sno
                     and cno='K002');

(4)查询没有选修K001和M001课程的学号、课程号和三次成绩(使用子查询)。

select sno,cno,score1,score2,score3
from work_info
where (sno not in (select sno from work_info
                      where cno='K001'
                         or cno='M001'));

2.数据操纵

(1)在学生表中添加一条学生记录,其中,学号为0593,姓名为张乐,性别为男,专业班级为电子05。

insert into student_info

values ('0593','张乐','男','电子05','','','');

(2)将所有课程的学分数变为原来的两倍。

update course_info

set c_score=2*c_score;

(3)删除张乐的信息。

delete from student_info where sname='张乐';

在C语言里,位左对齐右对齐一般在格式化输出时会用到,主要用于控制数据在输出时的位置。以下是相关介绍: ### 整型数据的左对齐右对齐 通过`printf`函数实现整型数据的左对齐右对齐右对齐是默认方式,在格式说明符`%`和`d`之间添加数字来规定输出宽度,若数字位数小于规定宽度,会在左边补空格;左对齐则需在数字前加`-`号,若数字位数小于规定宽度,会在右边补空格。 示例代码如下: ```c #include <stdio.h> int main() { // 右对齐。数字宽度为10,若不足10,在左边补足空格 printf("%10d\n", 1234); // 左对齐。数字宽度为10,若不足10,在右边补足空格 printf("%-10d\n", 1234); return 0; } ``` ### 不同输出长度的情况 当规定的输出宽度和数字实际位数不同时,有不同的处理方式。若规定宽度小于数字实际位数,会完整输出数字;若规定宽度大于数字实际位数,右对齐在左边补空格,左对齐在右边补空格。 示例代码如下: ```c #include <stdio.h> int main() { // -5是左对齐,输出长度为5。5是右对齐,输出长度为5 printf("%-5d %5d\n", 455, 455); printf("%-5d %5d\n", -123, -123); // 规定宽度小于实际位数,完整输出数字 printf("%-5d %5d\n", 987654, 987654); return 0; } ``` ### 其他数据类型的对齐 除整型外,其他数据类型也能实现左对齐右对齐。例如浮点数(`%f`)、字符串(`%s`)等,方法和整型一致。 示例代码如下: ```c #include <stdio.h> int main() { // 右对齐浮点数,宽度为10 printf("%10f\n", 3.14); // 左对齐浮点数,宽度为10 printf("%-10f\n", 3.14); // 右对齐字符串,宽度为10 printf("%10s\n", "hello"); // 左对齐字符串,宽度为10 printf("%-10s\n", "hello"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值