Concat and group_concat traps

Source: Internet
Author: User

Group_concat:

Connection trap of the 1.int Field

When using group_concat, note that if the connected field is of the int type, it must be converted to Char and then combined,
Otherwise, after you execute the statement (executescalar or any other method that executes the SQL return result), the returned result will not be a comma-separated string,
Instead, it is byte [].

This problem cannot be found in sqlyog or other tools.

Select group_concat (IPaddress) from t_ip returns comma-separated strings
Select group_concat (ID) from t_ip return byte []
Select group_concat (cast (ID as char) from t_dep returns comma-separated strings
Select group_concat (convert (ID, char) from t_dep returns comma-separated strings

Attached cast and convert usage:
Cast (expr as type), convert (expr, type), convert (expr using transcoding_name)
Cast () and convert () functions can be used to obtain values of one type and generate values of another type.

This type can be one of the following values:

Binary [(n)]
Char [(n)]
Date
Datetime
Decimal
Signed [integer]
Time
Unsigned [integer]

2. Length trap
When using group_concat to connect a field, there is a limit on the length, not the number of connections. But you can set it.

Using group_concat_max_len system variables, you can set the maximum allowed length.
ProgramThe syntax for this operation is as follows, where Val is an unsigned integer:
Set [session | Global] group_concat_max_len = val;
If the maximum length has been set, the result is ended with the maximum length.

Run set global group_concat_max_len = 10 in sqlyog and re-open sqlyog. The setting will take effect.

Note that this method is only temporary. to modify it for a long time, you need to modify the configuration section of MySQL.

Add group_concat_max_len = 99999 to the mysqld node of my. CNF ......

Restart MySQL.

 

Concat:

Used to connect fields or characters. If one of the joined fields is null, Concat returns NULL.

This is a very dangerous thing. I believe that most of the results that people who use Concat to connect multiple fields don't look like this:

Concat (A, B). If B is null, Concat (a, B) = NULL

What we want is:

Connect a and B. If B is null, it should be a after connection.

In this case, use concat_ws (separator, str1, str2 ,...)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.