Database Experiment 2: Data Query

Experiment 2 data query

one. Purpose

1. Understand other query methods and applications of data in the database;

2. Learn to implement various query requirements;

3. Learn the similarities and differences of various queries and the conversion methods between them.

2. Experimental content

On the basis of Experiment 1, practice the use of other query statements, including calculated columns, summation, maximum and minimum values, various selection conditions, character matching, grouping and sorting, join queries, nested queries and EXISTS queries, etc., and experience Learn the similarities, differences and conversions between various queries, understand the execution process of various queries, and lay a good foundation for simple and comprehensive applications.

1. Query the name SNAME of the supplier whose supplier number is S1, and the city where it is located: CITY

2. Query the part number whose color is red

3. Query the project name JNAME whose project location is Tianjin

4. Query the supplier number and name, using lowercase letters and uppercase letters to represent the supplier code.

5. Query the detailed information of the part and display the result attribute name in Chinese.

6. Query the supplier number SNO of supply engineering J1 part P1

7. Query the name and city of the supplier, and sort them in ascending order by city. Those in the same city are sorted in descending order by supplier name.

8. Query the project number of parts supplied by supplier S1

9. Query the average weight of various parts

10. Query the total number of parts

11. Query the part number, part name and color of all parts starting with the word "snail"

12. Query the total quantity of parts P3 supplied by each supplier

13. Supplier number SNO for supplying engineering J1 red parts

14. Name and quantity of various parts used in Project J2

15. Name of the project using parts produced in Shanghai

16. The use of parts for all projects (whether parts are used or not), including project code, project name, part code and part quantity

17. Identical part numbers supplied by suppliers S1 and S3

18. There is no project number for using parts produced in Tianjin

19. The project number of the red parts produced by the Tianjin supplier is not used

20. At least the engineering number Jno of all parts supplied by supplier S1 is used

3. Experimental process and code

1. Query the name SNAME of the supplier whose supplier number is S1, and the city where it is located: CITY

SELECT SNAME,CITY
FROM s
WHERE sno='s1';

2. Query the part number whose color is red

SELECT PNO 零件号码
FROM p
WHERE color='红';

3. Query the project name JNAME whose project location is Tianjin

SELECT JNAME
FROM j
WHERE city='天津';

4. Query the supplier number and name, using lowercase letters and uppercase letters to represent the supplier code.

--1)	小写字母表示供应商代码:
SELECT LOWER(SNO)小写字母表示供应商代码,SNAME 名称
FROM s
--2)	大写字母表示供应商代码:
SELECT UPPER(SNO)大写字母表示供应商代码,SNAME 名称
FROM s

5. Query the detailed information of the part and display the result attribute name in Chinese.

SELECT PNO 零件代码,PNAME 零件名,COLOR 颜色,WEIGHT 重量 
FROM p

6. Query the supplier number SNO of supply engineering J1 part P1

SELECT SNO
FROM spj
WHERE JNO='J1'AND PNO='P1';

7. Query the name and city of the supplier, and sort them in ascending order by city. Those in the same city are sorted in descending order by supplier name.

SELECT SNAME,CITY
FROM s
ORDER BY CITY,SNAME DESC;

If the sorting results feel different from what you are used to, it is caused by the MySQL encoding format. We only need to change the UTF encoding format to gb2312 (as shown in the figure below) and the sorting rule to gb2312_chinese_ci to achieve a seemingly normal sorting result.

8. Query the project number of parts supplied by supplier S1

SELECT DISTINCT JNO
FROM spj
WHERE SNO='S1';

9. Query the average weight of various parts

--按名字分类
SELECT PNAME,avg(WEIGHT)平均重量
FROM p
GROUP BY PNAME

10. Query the total number of parts

SELECT COUNT(*) 零件总数
FROM p

11. Query the part number, part name and color of all parts starting with the word "snail"

SELECT PNO 零件号,PNAME 零件名,COLOR 颜色
FROM p
WHERE PNAME LIKE'螺%';

12. Query the total quantity of parts P3 supplied by each supplier

SELECT  SNO,SUM(QTY)零件P3总数量
FROM spj
WHERE PNO='P3'
GROUP BY SNO
	--“=”可以用“in”来代替:
SELECT  SNO,SUM(QTY)零件P3总数量
FROM spj
WHERE PNO in ('P3')
GROUP BY SNO
	--“not in”也可以实现:
SELECT  SNO,SUM(QTY)零件P3总数量
FROM spj
WHERE PNO NOT in('P1','P2','P4','P5','P6')
GROUP BY SNO
--但是,怎样显示所有供应商呢(没有供应P3的显示为空)?这就需要进行s表与spj表的连接了
SELECT s1.SNO,SUM(QTY)零件P3总数量
FROM s s1 LEFT OUTER JOIN (
	SELECT * 
	FROM spj 
	WHERE PNO='P3') s2 ON s1.SNO=s2.SNO
GROUP BY SNO

13. Supplier number SNO for supplying engineering J1 red parts

--左外连接:
SELECT SNO
FROM p LEFT OUTER JOIN spj ON p.PNO=spj.PNO
WHERE spj.JNO='J1' AND p.color='红'
--等值连接:
SELECT sno
FROM p,spj
WHERE p.pno=spj.pno AND jno='j1' AND color='红'
--嵌套查询
SELECT sno
FROM spj
WHERE jno='j1' AND pno in(
						SELECT pno
						FROM p
							WHERE color='红')

14. Name and quantity of various parts used in Project J2

--左外连接:
SELECT p.PNAME,spj.QTY 
FROM spj LEFT OUTER JOIN p ON spj.PNO=P.PNO 
WHERE JNO='J2';
--等值连接:
SELECT PNAME 零件名,QTY 数量
FROM spj,p  
WHERE  spj.PNO=P.PNO and JNO='J2';

15. Name of the project using parts produced in Shanghai

--三个表的左外连接:
SELECT  DISTINCT j.JNAME
FROM spj LEFT OUTER JOIN s ON spj.SNO=s.SNO LEFT OUTER JOIN j ON spj.JNO=j.JNO
WHERE s.CITY='上海';
--三层嵌套查询:
SELECT DISTINCT JNAME
FROM j
WHERE jno in(SELECT jno 
            FROM spj 
            WHERE sno in(SELECT sno 
                         FROM s 
                         WHERE city='上海'));
--三个表的等值连接:
SELECT DISTINCT JNAME
FROM j,s,spj
WHERE j.JNO=spj.JNO AND spj.SNO=s.SNO AND s.CITY='上海'

16. The use of parts for all projects (whether parts are used or not), including project code, project name, part code and part quantity

SELECT  J1.JNO 项目代码,J1.JNAME 项目名称,SPJ1.PNO 零件代码,SPJ1.QTY 零件数量
FROM j J1  LEFT OUTER JOIN spj SPJ1 ON SPJ1.JNO=J1.JNO 
ORDER BY J1.JNO;

17. Identical part numbers supplied by suppliers S1 and S3

--内连接:
SELECT DISTINCT spj1.PNO 
FROM spj spj1  INNER JOIN  spj spj2 ON spj1.PNO=spj2.PNO 
WHERE spj1.SNO='S1' AND spj2.SNO='S3';
--旧式内连接:
SELECT DISTINCT `FIRST`.PNO
FROM spj FIRST,spj SECOND
WHERE `FIRST`.PNO=`SECOND`.PNO AND `FIRST`.SNO='S1'AND `SECOND`.SNO='S3'

18. There is no project number for using parts produced in Tianjin

--EXISTS
SELECT JNO 
FROM j  
WHERE NOT EXISTS (
						SELECT * 
						FROM spj 
						WHERE SNO IN (
									SELECT SNO 
									FROM s 
									WHERE CITY='天津') AND j.JNO=spj.JNO);
--嵌套查询:
SELECT jno
FROM j
WHERE jno not in(
		SELECT  DISTINCT JNO
		FROM spj
		WHERE sno in(SELECT sno 
					FROM s
					WHERE CITY='天津'));

19. The project number of the red parts produced by the Tianjin supplier is not used

--Exist方法:
SELECT JNO 
FROM j  
WHERE NOT EXISTS (
					SELECT * 
					FROM spj 
					WHERE SNO IN (
								SELECT SNO 
								FROM s 
								WHERE CITY='天津' ) AND PNO IN (
												    SELECT PNO 
												    FROM p 
										            WHERE COLOR='红') AND 
                                                    j.JNO=spj.JNO);
--嵌套查询:
SELECT jno
FROM j
WHERE jno not in(
				SELECT  DISTINCT JNO
				FROM spj
				WHERE sno in(SELECT sno 
									FROM s
									WHERE CITY='天津')
		  		 AND pno in(SELECT pno 
					        FROM p
					        WHERE color='红'));

20. At least the engineering number Jno of all parts supplied by supplier S1 is used

SELECT DISTINCT JNO
FROM spj spj1
WHERE NOT EXISTS
(SELECT * 
 FROM spj spj2 
 WHERE spj2.SNO='S1' AND NOT EXISTS
(SELECT * 
 FROM spj spj3 
 WHERE spj3.JNO=spj1.JNO AND spj3.PNO=spj2.PNO)
);

4. Experiment summary

  1. To query the column alias of the result, you only need to add a space and a column alias after the select column name.

        After the list item + space + [header name], you can also use as to replace the space.

  1. Use lowercase letters to represent a string using LOWER(SNO), and use uppercase letters to represent a string using UPPER(SNO).
  2. MySQL's character set and arrangement rules are different, and the sorting results are different.
  3. Function to find the average: avg()
  4. Function to count numbers: count()
  5. For fuzzy query, use LIKE, not "=", % represents any number of characters, and _ (underscore) represents the position of a character.
  6. Equivalent joins and left outer joins both fully join several tables and then query based on conditions.
  7. It is very convenient to use nested queries for many multi-table queries, which can be analyzed layer by layer, which is relatively easy. Note: Nested queries can only be used when the query results belong to a table.
  8. The principle of Exist query can be thought of as similar to a for loop. For example, in question 18, the query is entered in sequence from j1 to j7. Exist is used to determine whether it meets the conditions and meets the conditions of parts produced in Tianjin. exist is true and not exist is false. If it is not output and does not meet the conditions for parts produced in Tianjin, it can be output if not exist is true. (It’s just an understanding method, the expression may not be appropriate).

Guess you like

Origin blog.csdn.net/pzcxl/article/details/124661754