亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Java JavaBase What are the mysql query statements in java

What are the mysql query statements in java

Nov 02, 2020 pm 03:36 PM
java

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;].

What are the mysql query statements in java

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,&#39;SMITH&#39;,&#39;CLERK&#39;,7902,&#39;1980-12-17&#39;,800,NULL,20);
INSERT INTO emp VALUES(7499,&#39;ALLEN&#39;,&#39;SALESMAN&#39;,7698,&#39;1981-02-20&#39;,1600,300,30);
INSERT INTO emp VALUES(7521,&#39;WARD&#39;,&#39;SALESMAN&#39;,7698,&#39;1981-02-22&#39;,1250,500,30);
INSERT INTO emp VALUES(7566,&#39;JONES&#39;,&#39;MANAGER&#39;,7839,&#39;1981-04-02&#39;,2975,NULL,20);
INSERT INTO emp VALUES(7654,&#39;MARTIN&#39;,&#39;SALESMAN&#39;,7698,&#39;1981-09-28&#39;,1250,1400,30);
INSERT INTO emp VALUES(7698,&#39;BLAKE&#39;,&#39;MANAGER&#39;,7839,&#39;1981-05-01&#39;,2850,NULL,30);
INSERT INTO emp VALUES(7782,&#39;CLARK&#39;,&#39;MANAGER&#39;,7839,&#39;1981-06-09&#39;,2450,NULL,10);
INSERT INTO emp VALUES(7788,&#39;SCOTT&#39;,&#39;ANALYST&#39;,7566,&#39;1987-04-19&#39;,3000,NULL,20);
INSERT INTO emp VALUES(7839,&#39;KING&#39;,&#39;PRESIDENT&#39;,NULL,&#39;1981-11-17&#39;,5000,NULL,10);
INSERT INTO emp VALUES(7844,&#39;TURNER&#39;,&#39;SALESMAN&#39;,7698,&#39;1981-09-08&#39;,1500,0,30);
INSERT INTO emp VALUES(7876,&#39;ADAMS&#39;,&#39;CLERK&#39;,7788,&#39;1987-05-23&#39;,1100,NULL,20);
INSERT INTO emp VALUES(7900,&#39;JAMES&#39;,&#39;CLERK&#39;,7698,&#39;1981-12-03&#39;,950,NULL,30);
INSERT INTO emp VALUES(7902,&#39;FORD&#39;,&#39;ANALYST&#39;,7566,&#39;1981-12-03&#39;,3000,NULL,20);
INSERT INTO emp VALUES(7934,&#39;MILLER&#39;,&#39;CLERK&#39;,7782,&#39;1982-01-23&#39;,1300,NULL,10);
-- 向部門(mén)表添加數(shù)據(jù) , 采用批量插入數(shù)據(jù), 用 , 號(hào)隔開(kāi) .
INSERT INTO dept VALUES(10, &#39;ACCOUNTING&#39;, &#39;NEW YORK&#39;)
,(20, &#39;RESEARCH&#39;, &#39;DALLAS&#39;),(30, &#39;SALES&#39;, &#39;CHICAGO&#39;),
(40, &#39;OPERATIONS&#39;, &#39;BOSTON&#39;);
-- 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 &#39;1981-02-01&#39; AND &#39;1987-05-01&#39;;
-- 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(&#39;1981-__-__&#39;);
-- 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 &#39;__A%&#39;;
-- 10.  選擇姓名中有字母a和e的員工姓名
SELECT ename 員工姓名 FROM emp WHERE ename LIKE &#39;%A%&#39; OR &#39;%E%&#39;
-- 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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

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

How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

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.

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

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

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

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

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

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

Google Chrome cannot open local files Google Chrome cannot open local files Aug 01, 2025 am 05:24 AM

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

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

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

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

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

See all articles