Home > Database > Mysql Tutorial > body text

面试题中遇到的SQL题目_MySQL

WBOY
Release: 2016-06-01 13:26:55
Original
1038 people have browsed it

bitsCN.com

<br>1.假设有一张表示cj表 Name Subject Result张三 语文 80张三 数学 90张三 物理 85李四 语文 85李四 数学 92李四 物理 82要求查询结果:姓名 语文 数学 物理张三 80 90 85李四 85 92 82<br>
Copy after login
1 --创建cj表sql2 CREATE TABLE `cj` (3   `id` int(11) NOT NULL AUTO_INCREMENT,4   `name` varchar(20) DEFAULT NULL,5   `subject` varchar(20) DEFAULT NULL,6   `result` int(11) DEFAULT NULL,7   PRIMARY KEY (`id`)8 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
Copy after login
 1 --插入数据SQL 2 INSERT INTO cj 3    (`id`, `name`, `subject`, `result`) 4 VALUES 5    (1, '张三', '语文', 80); 6  7 INSERT INTO cj 8    (`id`, `name`, `subject`, `result`) 9 VALUES10    (2, '张三', '数学', 90);11 12 INSERT INTO cj13    (`id`, `name`, `subject`, `result`)14 VALUES15    (3, '张三', '物理', 85);16 17 INSERT INTO cj18    (`id`, `name`, `subject`, `result`)19 VALUES20    (4, '李四', '语文', 85);21 22 INSERT INTO cj23    (`id`, `name`, `subject`, `result`)24 VALUES25    (5, '李四', '数学', 92);26 27 INSERT INTO cj28    (`id`, `name`, `subject`, `result`)29 VALUES30    (6, '李四', '物理', 89);
Copy after login
1 --查询SQL2 select 3     distinct a.name 姓名,4     (select result from cj where name = a.name and subject = '语文') 语文,5     (select result from cj where name = a.name and subject = '数学') 数学,6     (select result from cj where name = a.name and subject = '物理') 物理   7 from cj a;
Copy after login

 

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template