Detailed explanation of the order of Mysql query results according to the order of ID in in()

Detailed explanation of the order of Mysql query results according to the order of ID in in()

Detailed explanation of the order of Mysql query results according to the order of ID in in()

Example code:

<select id="queryGBStyleByIDs" resultMap="styleMap"> 
    select style_num_id ,style_id,style_title,style_pic FROM gb_style where online = 1 AND is_hide = 0 and style_num_id in 
    <foreach collection="styleNumIDs" item="styleNumId" separator="," open="(" close=")"> 
     #{styleNumId} 
    </foreach> 
    ORDER BY FIELD 
    <foreach collection="styleNumIDs" item="styleNumId" separator="," open="(style_num_id," close=")"> 
      #{styleNumId} 
    </foreach> 
  </select> 

The final output sql is as follows:

select style_num_id ,style_id,style_title,style_pic FROM gb_style where online = 1 AND is_hide = 0 and style_num_id in (1,3,2,5) 
order by field (style_num_id,1,3,2,5);

If you have any questions, please leave a message or come to the community to discuss. Thank you for reading and I hope it can help you. Thank you for your support of this site!

You may also be interested in:
  • MySQL query in operation query results are displayed in the order of in set
  • Solve the problem that IN subquery in MySQL will cause the index to be unusable
  • Examples of optimization techniques for slow query efficiency in MySQL IN statements
  • Comparison of the efficiency of using or, in and union all in MySQL query commands
  • Mysql subquery IN using LIMIT application example
  • Will the index be used in the MySQL query condition?
  • A brief discussion on the efficiency of MySQL subquery union and in
  • Implementation of MySQL select in subquery optimization
  • MySQL SQL Optimization Tutorial: IN and RANGE Queries

<<:  One question to understand multiple parameters of sort command in Linux

>>:  Detailed explanation of CocosCreator message distribution mechanism

Recommend

Solve the cross-domain problem of get and post requests of vue $http

Vue $http get and post request cross-domain probl...

HTML table tag tutorial (3): width and height attributes WIDTH, HEIGHT

By default, the width and height of the table are...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

Resolving MySQL implicit conversion issues

1. Problem Description root@mysqldb 22:12: [xucl]...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

Vue implements verification whether the username is available

This article example shares the specific code of ...

Building a KVM virtualization platform on CentOS7 (three ways)

KVM stands for Kernel-based Virtual Machine, whic...

Detailed explanation of Angular component life cycle (I)

Table of contents Overview 1. Hook calling order ...

JavaScript data structure bidirectional linked list

A singly linked list can only be traversed from t...

Detailed examples of ajax usage in js and jQuery

Table of contents Native JS How to send a get req...

A complete list of commonly used Linux commands (recommended collection)

Table of contents 1. System Information 2. Shutdo...

Docker image loading principle

Table of contents Docker images What is a mirror?...

Summary of some of my frequently used Linux commands

I worked in operations and maintenance for two ye...