MYSQL string forced conversion method example

MYSQL string forced conversion method example

Preface

Since the types of the same fields in the two tables are different, or even the encoding types are different, the query will not use the index and the speed will be very slow.

Forced transfer

Let me give you a direct example:

A table id is int type B table id is char type

If you want to convert to int, it is signed

If you want to convert to char, then char

Note: The type to be converted must be the field of the table after the left join, otherwise the index will not be used.

Because the field types of the joint table are inconsistent, the index is not used.

select t.* from A tleft join B t1 on t.id = t1.id

The first conversion type

select t.* from A tleft join B t1 on t.id = cast(t1.id as signed)

The second conversion type

select t.* from A tleft join B t1 on t.id = convert(t1.id, signed)

ab=fg

However, the two fields b and g have different sorting rules. b is utf8_general_ci, and g is utf8_unicode_ci. If they are equal, SQL will report an error. So I asked my superior for help. The way to write it is:

CONVERT (ab USING utf8) COLLATE utf8_unicode_ci = fg

If the table is not indexed, the fields are transformed in the query, for example:

select a.* from a
LEFT JOIN (SELECT b.*, CONVERT (ab USING utf8) COLLATE utf8_unicode_ci = fg FROM b) a ON ab = fg

CONVERT(user_id USING utf8) COLLATE utf8_general_ci as user_id

This is the end of this article about MYSQL string conversion. For more information about MYSQL string conversion, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of MySQL date string timestamp conversion
  • Convert image gif, jpg or mysql longblob or blob field value into hexadecimal string in php
  • Example of converting the mysql string '123' to the number 123

<<:  Research on the Input Button Function of Type File

>>:  Docker implements re-tagging and deleting the image of the original tag

Recommend

Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14

1. RTMP RTMP streaming protocol is a real-time au...

Install .NET 6.0 in CentOS system using cloud server

.NET SDK Download Link https://dotnet.microsoft.c...

Docker uses root to enter the container

First run the docker container Run the command as...

Complete steps to implement location punch-in using MySQL spatial functions

Preface The project requirement is to determine w...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...

Docker installation and configuration steps for Redis image

Table of contents Preface environment Install Cre...

Detailed explanation of Alibaba Cloud security rule configuration

Two days ago, I took advantage of the Double 11 s...

Detailed explanation of javascript knowledge points

Table of contents 1. Basic Introduction to JavaSc...

Detailed explanation of the properties and functions of Vuex

Table of contents What is Vuex? Five properties o...

Mini Programs enable product attribute selection or specification selection

This article shares the specific code for impleme...

The difference between html block-level tags and inline tags

1. Block-level element: refers to the ability to e...

Some suggestions for ensuring MySQL data security

Data is the core asset of an enterprise and one o...