หน้านี้ยังไม่พร้อมใช้งานในภาษาท้องถิ่นของคุณ เรากำลังพยายามอย่างหนักเพื่อเพิ่มเวอร์ชันภาษาอื่น ๆ เพิ่มเติม ขอบคุณสำหรับการสนับสนุนเสมอมา

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

SELECT

Updated on 2025-01-20 GMT+08:00

SELECT

  • Syntax Format
    1
    2
    3
    4
    5
    6
    SELECT [ ALL | DISTINCT ]
      { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY { groupItem [, groupItem ]* } ]
      [ HAVING booleanExpression ]
    
  • Syntax Description

    SELECT is used to select data from a table.

    ALL indicates that all results are returned.

    DISTINCT indicates that the duplicated results are removed.

  • Precautions
    • The table to be queried must exist. Otherwise, an error is reported.
    • WHERE is used to specify the search condition, which can be the arithmetic operator, relational operator, or logical operator.
    • GROUP BY is used to specify the grouping field, which can be one or more multiple fields.
  • Example

    Select the order which contains more than 3 pieces of data.

    1
    SELECT  * FROM Orders WHERE units > 3; 
    

    Queries a group of constant data.

    1
    select 'Lily', 'male', 'student', 17;
    

WHERE

  • Syntax Format
    1
    2
    3
    SELECT   { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
    
  • Syntax Description

    This clause is used to filter the query results using the WHERE clause.

  • Precautions
    • The to-be-queried table must exist.
    • WHERE filters the records that do not meet the requirements.
  • Example

    Search orders which contain more than 3 pieces and fewer than 10 pieces of data.

    1
    2
    SELECT  * FROM Orders
      WHERE units > 3 and units < 10; 
    

HAVING

  • Function

    This clause is used to search for the query results that meet the search condition.

  • Syntax Format
    1
    2
    3
    4
    5
    SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY { groupItem [, groupItem ]* } ]
      [ HAVING booleanExpression ]
    
  • Syntax Description

    Generally, HAVING and GROUP BY are used together. You can use GROUP BY for grouping and then use HAVING for filtering. Arithmetic operations and aggregate functions are supported in the HAVING clause.

  • Precautions

    If the filtering condition is subject to the results of GROUP BY, the HAVING clause, rather than the WHERE clause, must be used for search.

  • Example

    Group the student table according to the name field and search for the records in which the maximum score is higher than 95 in the group.

    1
    2
    3
    SELECT name, max(score) FROM student
      GROUP BY name
      HAVING max(score) >95;
    

Column-Based GROUP BY

  • Function

    This clause is used to group a table based on columns.

  • Syntax Format
    1
    2
    3
    4
    SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY { groupItem [, groupItem ]* } ]
    
  • Syntax Description

    Column-based GROUP BY can be categorized into single-column GROUP BY and multi-column GROUP BY.

    • Single-column GROUP BY indicates that the GROUP BY clause contains only one column.
    • Multi-column GROUP BY indicates that the GROUP BY clause contains multiple columns. The table will be grouped according to all fields in the GROUP BY clause. The records whose fields are the same are grouped into one group.
  • Precautions

    GroupBy generates update results in the stream processing table.

  • Example

    Group the student table according to the score and name fields and return the grouping results.

    1
    2
    SELECT name,score, max(score) FROM student 
      GROUP BY name,score;
    

Expression-Based GROUP BY

  • Function

    This clause is used to group streams according to expressions.

  • Syntax Format
    1
    2
    3
    4
    SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY { groupItem [, groupItem ]* } ]
    
  • Syntax Description

    groupItem can have one or more fields. The fields can be called by string functions, but cannot be called by aggregate functions.

  • Precautions

    None

  • Example

    Use the substring function to obtain the character string from the name field, group the student table according to the obtained character string, and return each sub character string and the number of records.

    1
    2
    SELECT substring(name,6),count(name) FROM student
      GROUP BY substring(name,6);
    

Grouping sets, Rollup, Cube

  • Function
    • The GROUP BY GROUPING SETS generates a result set equivalent to that generated by multiple simple GROUP BY UNION ALL statements. Using GROUPING SETS is more efficient.
    • The ROLLUP and CUBE generate multiple groups based on certain rules and then collect statistics by group.
    • The result set generated by CUBE contains all the combinations of values in the selected columns.
    • The result set generated by ROLLUP contains the combinations of a certain layer structure in the selected columns.
  • Syntax Format
    1
    2
    3
    4
    SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY groupingItem]
    
  • Syntax Description

    Values of groupingItem can be Grouping sets(columnName [, columnName]*), Rollup(columnName [, columnName]*), and Cube(columnName [, columnName]*).

  • Precautions

    None

  • Example

    Return the results generated based on user and product.

    1
    2
    3
    SELECT SUM(amount)
    FROM Orders
    GROUP BY GROUPING SETS ((user), (product));
    

GROUP BY Using HAVING

  • Function

    This clause filters a table after grouping it using the HAVING clause.

  • Syntax Format
    1
    2
    3
    4
    5
    SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
      FROM tableExpression
      [ WHERE booleanExpression ]
      [ GROUP BY { groupItem [, groupItem ]* } ]
      [ HAVING booleanExpression ]
    
  • Syntax Description

    Generally, HAVING and GROUP BY are used together. You can use GROUP BY for grouping and the HAVING for filtering.

  • Precautions
    • If the filtering condition is subject to the results of GROUP BY, the HAVING clause, rather than the WHERE clause, must be used for search. HAVING and GROUP BY are used together. Use GROUP BY for grouping and the HAVING for filtering.
    • Fields used in HAVING, except for those used for aggregate functions, must exist in GROUP BY.
    • The arithmetic operation and aggregate function are supported by the HAVING clause.
  • Example

    Group the transactions by num, use the HAVING clause to search for the records in which the maximum value derived from multiplying price with amount is higher than 5000, and return the filtered results.

    1
    2
    3
    4
    SELECT num, max(price*amount) FROM transactions
      WHERE time > '2016-06-01'
      GROUP BY num
      HAVING max(price*amount)>5000;
    

เราใช้คุกกี้เพื่อปรับปรุงไซต์และประสบการณ์การใช้ของคุณ การเรียกดูเว็บไซต์ของเราต่อแสดงว่าคุณยอมรับนโยบายคุกกี้ของเรา เรียนรู้เพิ่มเติม

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback