mysql query statements in java: 1. Simple query; 2. Simple query; 3. Sorting query; 4. Group query, the code is [group by grouped field.[Having condition]]; 5. For paging query, the code is [select * from table name limit x;].
mysql query statement in java:
1. Simple query
–Query all fields:
select * from table name;
- - Query specified fields:
select field 1, field 2... from table name;
- - Table alias: If there are special symbols or spaces in the alias, it needs to be enclosed in quotation marks
select * from table name [as] Table alias
- - Column Alias: as can be omitted.
select field 1 [as] alias, field 2 [as] alias from table name;
- - Remove duplicate values: If there are multiple fields, they must be Repeat.
select distinct field from table name;
- - Operation query:
select (math english) total score from table name;
2. Conditional query:
Comparison operators: > , < , = , >= , <= , <>(!=)
Logical operators:
between…and… : Values ??displayed in a certain interval (including head and tail)
in (multiple conditions) : or (or) relationship
like: Fuzzy query
% represents zero or more arbitrary characters.
_ represents one character.
is null: Determine whether it is empty.
3. Sorting query: Write at the end of the sql statement.
select * from table name order by sorting field ASC (ascending order - default)/DESC (descending order)
If there are multiple fields to be sorted, sort them by the first one first, and then sort by the following ones
4. Aggregation function: after select, before from.
sum (sum): The specified column is not a numeric type, and the calculation result is 0;
count (statistical number): does not include null; generally use *;
max (maximum value ): If it is a string type, use string sorting;
min(minimum value) :
avg(average): The specified column is not a numeric type, and the calculation result is 0;
5. Group query
group by 被分組的字段.[Having 條件]
where: Filter before group query.
having: Group query Post-filtering.
Note; Grouped fields are generally written after select as query conditions for easy viewing
6. Paging query (understand)
Use keyword limit
Format one: only the first x data
select * from 表名 limit x;
Format two: paging query
select * from 表名 limit m,n;
m: The starting number of rows of data per page, changing
n: The number displayed on each page, fixed
Note:
The index of the row in the database starts from 0
The index of the column starts from 1
Single table case:
-- 創(chuàng)建數(shù)據(jù)庫(kù) create database day03; -- 員工表 USE day03; CREATE TABLE emp( -- 員工編號(hào) empno INT, -- 員工姓名 ename VARCHAR(50), -- 工作 job VARCHAR(50), -- 管理者 mgr INT, -- 雇用時(shí)間 hiredate DATE, -- 工資 sal DECIMAL(7,2), -- 獎(jiǎng)金 comm DECIMAL(7,2), -- 部門(mén) deptno INT ) ; -- 部門(mén)表 CREATE TABLE dept( -- 部門(mén) deptno INT, -- 部門(mén)名稱(chēng) dname VARCHAR(14), -- 部門(mén)位置 loc VARCHAR(13) ); -- 向員工表中添加數(shù)據(jù). INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20); INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30); INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30); INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20); INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30); INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30); INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10); INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20); INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10); INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30); INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20); INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30); INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20); INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10); -- 向部門(mén)表添加數(shù)據(jù) , 采用批量插入數(shù)據(jù), 用 , 號(hào)隔開(kāi) . INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK') ,(20, 'RESEARCH', 'DALLAS'),(30, 'SALES', 'CHICAGO'), (40, 'OPERATIONS', 'BOSTON'); -- 1. 查詢(xún)工資大于1200的員工姓名和工資 SELECT ename 員工姓名 , sal 工資 FROM emp WHERE sal > 1200; -- 2. 查詢(xún)員工號(hào)為7698的員工的姓名和部門(mén)號(hào) SELECT ename 員工姓名 , deptno 部門(mén)號(hào) FROM emp WHERE empno = 7698; -- 3. 選擇工資不在500到1200的員工的姓名和工資 SELECT ename 員工姓名 ,sal 工資 FROM emp WHERE sal<=1200 && sal >= 500; -- 4. 選擇雇用時(shí)間在1981-02-01到1987-05-01之間的員工姓名,job_id和雇用時(shí)間 SELECT ename 員工姓名,empno , hiredate FROM emp WHERE hiredate BETWEEN '1981-02-01' AND '1987-05-01'; -- 5. 選擇在20或30號(hào)部門(mén)工作的員工姓名和部門(mén)號(hào) SELECT ename 員工姓名,deptno 部門(mén)號(hào) FROM emp WHERE deptno IN(20 , 30 ); -- 6. 選擇在1981年雇用的員工的姓名和雇用時(shí)間 SELECT ename 員工姓名,hiredate 雇傭時(shí)間 FROM emp WHERE hiredate LIKE('1981-__-__'); -- 7. 選擇公司中沒(méi)有管理者的員工姓名及job_id SELECT ename 員工姓名,empno FROM emp WHERE mgr IS NULL; -- 8. 選擇公司中有獎(jiǎng)金的員工姓名,工資和獎(jiǎng)金級(jí)別 SELECT ename 員工姓名,sal 工資,comm 獎(jiǎng)金 FROM emp WHERE comm IS NOT NULL OR; -- 9. 選擇員工姓名的第三個(gè)字母是a的員工姓名 SELECT ename 員工姓名 FROM emp WHERE ename LIKE '__A%'; -- 10. 選擇姓名中有字母a和e的員工姓名 SELECT ename 員工姓名 FROM emp WHERE ename LIKE '%A%' OR '%E%' -- 11. 查詢(xún)員工號(hào),姓名,工資,以及工資提高百分之20%后的結(jié)果(new salary) SELECT empno 員工號(hào), ename 姓名,sal+(sal*0.2) 工資 FROM emp; -- 12. 將員工的姓名按首字母排序 SELECT ename FROM emp ORDER BY ename ASC; -- 升序 SELECT ename FROM emp ORDER BY ename DESC; -- 降序 -- 13. 查詢(xún)公司員工工資的最大值,最小值,平均值,總和 SELECT MAX(sal) 最大值,MIN(sal) 最小值 , AVG(sal) 平均值, SUM(sal) 總和 FROM emp; -- 14. 查詢(xún)各deptno的員工工資的最大值,最小值,平均值,總和 SELECT deptno 部門(mén),MAX(sal) 最大值,MIN(sal) 最小值 , AVG(sal) 平均值, SUM(sal) 總和 FROM emp GROUP BY deptno; -- 15. 選擇具有各個(gè)deptno的員工人數(shù) SELECT deptno , COUNT(empno) FROM emp GROUP BY deptno;
Related free learning recommendations: java basic tutorial, mysql video tutorial
The above is the detailed content of What are the mysql query statements in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

ChromecanopenlocalfileslikeHTMLandPDFsbyusing"Openfile"ordraggingthemintothebrowser;ensuretheaddressstartswithfile:///;2.SecurityrestrictionsblockAJAX,localStorage,andcross-folderaccessonfile://;usealocalserverlikepython-mhttp.server8000tor

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,
