Batch Select
- This query retrieves all records from the
students
table where thestudent_name
matches one of the specified values:'Xinyang'
,'Ruihan'
, or'Caomei'
in one execution
Code
The
IN
operator allows for filtering multiple values in a single query, making it efficient for batch selection.
Java Mybatis code example
The above method selects a batch of label values from the
metric_label_lookup
index table where the label values match the given error codes.
Batch Insert
- This query inserts multiple records into the
students
table in a single execution
Code
The
VALUES
clause specifies the data for each new record, including thename
,gender
, andheight
columns. Batch insertion like this reduces the overhead compared to inserting each record individually.
Java Mybatis code example
Inserts a batch of error codes into the metric_label_lookup table.
Batch Update
- This query updates multiple records and columns in the
students
table in a single execution. This approach minimizes the need for multiple update queries by combining them into a single query
Code
The
CASE
statement dynamically assigns different values to theheight
column based on thename
of the student.The
gender
column is set to'Male'
for all matching records.The
WHERE
clause ensures that only rows where thename
is'Xinyang'
or'Ruihan'
are updated.
Java Mybatis code example
Updates the columns in the metric_label_lookup table for the given list of error codes.
Batch Upsert
- Batch upsert, which combines the operations of Batch Insert and Batch Update
Code
If a record with the same unique or primary key already exists, the
ON DUPLICATE KEY UPDATE
clause updates the existing record with new values.If no matching record exists, a new row is inserted.
The
VALUES()
function retrieves the values from theINSERT
statement for updating existing records.
Important
In MySQL, to use a non-primary column (like
name
) to check for record existence, you must define a unique constraint on that column in the table schema.