Several useful MySQL functions

Tram Ho

Querying out data and then processing it in a programming language is cool, but did you know that MySQL has provided some of these useful functions for us as well?

CHARACTER_LENGTH

Actually, it is just the same

we will write instead

Hmm actually I think this MySql style is longer than that but it’s nice to know one more new one.

FORMAT

Usage is the same as CHARACTER_LENGTH. But the use is completely different

number: the number we need to format
decimal_places: hearing the name, we type is the position of the decimal number, but you just understand that the number of decimal places is simply added. For example

TRIM

Remove terminal whitespace.
This is quite good because if you query out a Post object and have a content field that needs to trim the title in many places, instead of everywhere, post.content.strip prone to miss, you can trim right at the example query. as

REPLACE

Its use is similar to ruby’s gsub – Replace all a character or string with another character or string.

For example:

GREATEST and LEAST

Returns the maximum and minimum value. For example:

DATE_FORMAT

Convert format of date data type Example:

CASE

This I think everyone should know because of it, it supports me a lot in cases of selecting according to conditions.
For example:

OrderIDQuantityQuantityText
firsttwelfthThe quantity is under 30
240The quantity is greater than 30
3tenThe quantity is under 30

CAST

Converts values ​​to a specific data type.
For example:

COALESCE

Returns the first value that is not Nulled

IF

Too familiar but its syntax is like this

summary

Here are some functions that I myself think will be useful (in a bunch of available MySQL functions). Hope it helps everyone too (bow)

Share the news now

Source : Viblo