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

Implementation of Docker deployment of web projects

The previous article has installed the docker ser...

js uses the reduce method to make your code more elegant

Preface In actual projects, the most common proce...

Vue.js handles Icon icons through components

Icon icon processing solution The goal of this re...

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...

Vue implements the magnifying glass effect of tab switching

This article example shares the specific code of ...

CSS and HTML and front-end technology layer diagram

Front-end technology layer (The picture is a bit e...

About the problem of dynamic splicing src image address of img in Vue

Let's take a look at the dynamic splicing of ...

Using streaming queries in MySQL to avoid data OOM

Table of contents 1. Introduction 2. JDBC impleme...

Pycharm2017 realizes the connection between python3.6 and mysql

This article shares with you how to connect pytho...

Implementation of local migration of docker images

I've been learning Docker recently, and I oft...

How does MySQL achieve master-slave synchronization?

Master-slave synchronization, also called master-...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

HTML form tag tutorial (2):

This tutorial introduces the application of vario...

How to use vue-video-player to achieve live broadcast

Table of contents 1. Install vue-video-player 2. ...

Sample code for CSS dynamic loading bar effect

Using the knowledge of CSS variables, I will dire...