In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Create student and score tables
CREATE TABLE student (id INT (10) NOT NULL UNIQUE PRIMARY KEY, name VARCHAR (20) NOT NULL, sex VARCHAR (4), birth YEAR,department VARCHAR (20), address VARCHAR (50))
Create a score table. The SQL code is as follows:
CREATE TABLE score (id INT (10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT, stu_id INT (10) NOT NULL, c_name VARCHAR (20), grade INT (10)
two。 Add records to student and score tables
The INSERT statement to insert a record into the student table is as follows:
INSERT INTO student VALUES (901, 'Zhang Boss', male, 1985, 'computer Department', 'Haidian District, Beijing'); INSERT INTO student VALUES (902, Zhang Laoer, male, 1986, Chinese Department, Changping District, Beijing); INSERT INTO student VALUES (903, Zhang San, female, 1990, Yongzhou, Hunan Province) INSERT INTO student VALUES (904, Li Si, male, 1990, English Department, Fuxin, Liaoning Province); INSERT INTO student VALUES (905, Wang Wu, female, 1991, English Department, Xiamen, Fujian Province); INSERT INTO student VALUES (906, Wang Liu, male, 1988 computer Department, Hengyang City, Hunan Province)
The INSERT statement to insert a record into the score table is as follows:
INSERT INTO score VALUES (NULL,901, computer, 98); INSERT INTO score VALUES (NULL,901, English, 80); INSERT INTO score VALUES (NULL,902, computer, 65); INSERT INTO score VALUES (NULL,902, Chinese, 88); INSERT INTO score VALUES (NULL,903, Chinese, 95); INSERT INTO score VALUES (NULL,904, computer, 70); INSERT INTO score VALUES (NULL,904, English, 92) INSERT INTO score VALUES (NULL,905, English, 94); INSERT INTO score VALUES (NULL,906, computer, 90); INSERT INTO score VALUES (NULL,906, English, 85)
3. Query all records of the student table
Mysql > SELECT * FROM student +-+ | id | name | sex | birth | department | address | +-+-- -+ | 901 | Zhang Laodong | male | 1985 | Department of computer Science | Haidian District of Beijing | | 1986 | Chinese Department | Changping District of Beijing | | 903 | Zhang San | female | 1990 | Chinese Department | Yongzhou City, Hunan Province | | 1990 | male | 1990 | English Department | Fuxin City, Liaoning Province | | 905 | Wang Wu | female | 1991 | English | Language Department | Xiamen City, Fujian Province | | 906 | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | +-+-+
4. Query articles 2 to 4 records of the student table
Mysql > SELECT * FROM student LIMIT 1pr 3 +-+ | id | name | sex | birth | department | address | +-+-- -+ | 902 | Zhang Laoer | male | 1986 | Chinese Department | Changping District, Beijing | | 903 | Zhang San | female | 1990 | Chinese Department | Yongzhou City, Hunan Province | | 1990 | male | 1990 | English Department | Fuxin City, Liaoning Province | +- -+
5. Query the information of all students' student numbers (id), names (name) and departments (department) from the student table
Mysql > SELECT id,name,department FROM student +-+ | id | name | department | +-+ | 901 | Zhang Boss | Department of computer Science | | 902 | Zhang Laoer | Chinese Department | | 903 | Zhang San | Chinese Department | | 904 | Li Si | English Department | | 905 | Wang Wu | English | Department | | 906 | Wang Liu | Department of computer Science | +-+
6. Query the information of computer and English majors from the student table
Mysql > SELECT * FROM student WHERE department IN ('computer Department', 'English Department') +-+ | id | name | sex | birth | department | address | +-+-- -+ | 901 | Zhang Boss | male | 1985 | Department of computer Science | Haidian District of Beijing | | 904 | Lisi | male | 1990 | English Department | Fuxin City, Liaoning Province | | 905 | Wang Wu | female | 1991 | English Department | Xiamen City, Fujian Province | | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | +-+- -- +
7. Query the information of students aged 18 to 22 from the student table
Mysql > SELECT id,name,sex,2013-birth AS age,department,address
-> FROM student
-> WHERE 2013-birth BETWEEN 18 AND 22
+-+ +
| | id | name | sex | age | department | address | |
+-+ +
| | 905 | Wang Wu | female | 22 | English Department | Xiamen City, Fujian Province |
+-+ +
Mysql > SELECT id,name,sex,2013-birth AS age,department,address
-> FROM student
-> WHERE 2013-birth > = 18 AND 2013-birth SELECT department, COUNT (id) FROM student GROUP BY department
+-+ +
| | department | COUNT (id) |
+-+ +
| Department of computer Science | 2 |
| | English Department | 2 |
| | Chinese Department | 2 |
+-+ +
9. Query the highest score for each subject from the score table
Mysql > SELECT cymnameMAX (grade) FROM score GROUP BY cymnamethorax + | c_name | MAX (grade) | +-+-+ | computer | 98 | | English | 94 | Chinese | 95 | +-+-+ |
10. Check Li Si's examination subjects (c_name) and test results (grade)
Mysql > SELECT c_name, grade-> FROM score WHERE stu_id=-> (SELECT id FROM student-> WHERE name='Li Si'); +-+-+ | c_name | grade | +-+-+ | computer | 70 | | English | 92 | +-+-+
11. Query all the students' information and examination information by connection.
Mysql > SELECT student.id,name,sex,birth,department,address,c_name,grade-> FROM student,score-> WHERE student.id=score.stu_id +-+ | id | name | sex | birth | department | address | c_name | grade | +- -+ | 901 | Zhang Boss | male | 1985 | computer Department | Beijing Haidian District | computer | 98 | | 901 | Zhang Boss | male | 1985 | computer Department | Beijing Haidian District | English | 80 | 902 | Zhang Laoer | Male | 1986 | Chinese Department | Beijing Changping District | computer | 65 | | 902 | Zhang Laoer | male | 1986 | Chinese Department | Beijing Changping District | Chinese | 88 | Zhang San | female | 1990 | Chinese Department | Yongzhou City, Hunan Province | Chinese | 95 | Lisi | male | 1990 | English Department | Fuxin City, Liaoning Province | computer | 70 | 904 | 904 | Li Si | male | 1990 | English Department | Fuxin City, Liaoning Province | English | 92 | | 905 | Wang Wu | female | 1991 | English Department | Xiamen City, Fujian Province | English | 94 | | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | computer | 90 | | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | English | 85 | +-+ +
twelve。 Calculate the total score of each student
Mysql > SELECT student.id,name,SUM (grade) FROM student,score-> WHERE student.id=score.stu_id-> GROUP BY id +-+ | id | name | SUM (grade) | +-+ | 901 | Zhang Laobao | 178 | 902 | Zhang Laoer | 153 | 903 | Zhang San | 95 | 904 | Li Si | 162 | 905 | Wang Wu | 94 | | 906 | Wang Liu | 175 | +-+
13. Calculate the average score of each examination subject
Mysql > SELECT cymnamePowerAVGAVG (grade) FROM score GROUP BY cymnametre maitrawi + | c_name | AVG (grade) | +-+-+ | computer | 80.7500 | | English | 87.7500 | Chinese | 91.5000 | +-+-+ |
14. Query the information of students whose computer scores are less than 95.
Mysql > SELECT * FROM student-> WHERE id IN-> (SELECT stu_id FROM score-> WHERE censor name = "computer" and grade SELECT * FROM student-> WHERE id = ANY-> (SELECT stu_id FROM score-> WHERE stu_id IN (- > SELECT stu_id FROM-> score WHERE camee = 'computer')-> AND censor name = 'English') +-+ | id | name | sex | birth | department | address | +-+-- -+ | 901 | Zhang Boss | male | 1985 | Department of computer Science | Haidian District, Beijing | | 904 | Lisi | male | 1990 | English Department | Fuxin City, Liaoning Province | | 1988 | male | 1988 | computer Department | Hengyang City, Hunan Province | +- -+
Mysql > SELECT a.* FROM student a, score b, score c
-> WHERE a.id=b.stu_id
-> AND b.clocknameplate 'computer'
-> AND a.id=c.stu_id
-> AND c.clockname 'English'
+-+ +
| | id | name | sex | birth | department | address | |
+-+ +
| | 1985 | Zhang Boss | male | 1985 | Department of computer Science | Haidian District of Beijing | | 904 | Lisi | male | 1990 | English Department | Fuxin City, Liaoning Province | | 1988 | male | 1988 | Department of computer Science | Hengyang City, Hunan Province | +-+-+ |
16. Sort computer test scores from high to low
Mysql > SELECT stu_id, grade-> FROM score WHERE clockname = 'computer'-> ORDER BY grade DESC;+-+-+ | stu_id | grade | +-+-+ | 901 | 98 | | 906 | 90 | | 904 | 70 | | 902 | 65 | +-+-+ |
17. Query the student's student number from student table and score table, and then merge the query results
Mysql > SELECT id FROM student-> UNION-> SELECT stu_id FROM score;+-+ | id | +-+ | 901 | | 902 | | 904 | | 905 | | 906 | +-+ |
18. Inquire about the names, departments, examination subjects and scores of students surnamed Zhang or Wang
Mysql > SELECT student.id, name,sex,birth,department, address, clockname grade-> FROM student, score-> WHERE-> (name LIKE 'Zhang%' OR name LIKE 'King%')-> AND-> student.id=score.stu_id +-+ | id | name | sex | birth | department | address | c_name | grade | +- -+ | 901 | Zhang Boss | male | 1985 | computer Department | Beijing Haidian District | computer | 98 | | 901 | Zhang Boss | male | 1985 | computer Department | Beijing Haidian District | English | 80 | 902 | Zhang Laoer | Male | 1986 | Chinese Department | Beijing Changping District | computer | 65 | | 902 | Zhang Laoer | male | 1986 | Chinese Department | Beijing Changping District | Chinese | 88 | Zhang San | female | 1990 | Chinese Department | Yongzhou City, Hunan Province | Chinese | 95 | Wang Wu | female | 1991 | English Department | Xiamen City, Fujian Province | English | 94 | 94 | 906 | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | computer | 90 | | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | English | 85 | +- -+
19. The inquiries are all about the names, ages, departments, examination subjects and scores of the students in Hunan.
Mysql > SELECT student.id, name,sex,birth,department, address, clockname grade-> FROM student, score-> WHERE address LIKE 'Hunan%' AND-> student.id=score.stu_id +-+ | id | name | sex | birth | department | address | c_name | grade | +- -+ | 903 | Zhang San | female | 1990 | Chinese Department | Yongzhou City, Hunan Province | Chinese | 95 | | 906 | Wang Liu | male | 1988 | computer Department | Hengyang City, Hunan Province | computer | 90 | | Wang Liu | male | 1988 | computer | Department | Hengyang City, Hunan Province | English | 85 | +-+
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.