Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The problem of using multiple single quotes and three quotes in MySQL concat

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

We often use character concatenation when dynamically stitching strings. I don't understand the quotation marks of concatenation, such as:

1. Why are there three quotation marks in''+ id +''? why is there a plus sign on the left and a plus sign on the right (can you explain this for me in more detail?)

SQL code

Sum (case Leave when''+ id +''then DaysNo else 0 end) [' + name +']'

Normal statement:

SQL code

SUM (CASE Leave WHEN '01' THEN DaysNo END) AS [personal leave]

SUM (CASE Leave WHEN '02' THEN DaysNo END) AS [sick leave]

Brief answer:

To put it simply, generally speaking, single quotation marks are used for concatenation statements, and three quotation marks are string variable values.

Because quotation marks have a special meaning, they have to be written twice to escape.

2. Is there any difference between single quotation marks and double quotation marks?

To put it simply, use single quotation marks if you use them normally, and use single quotation marks if you enclose them. Then add an additional single quote (used by escape) and change it to three single quotation marks.

In standard SQL, strings use single quotation marks.

If the string itself includes single quotes, use two single quotes (note that they are not double quotes, and double quotes in the string do not need to be escaped).

MySQL's extension to SQL allows the use of both single and double quotes.

A brief summary:

When spelling strings through concat, if you need to use single quotation marks in the statement, you can use two single quotation marks instead of one.

Mysql > select concat ('drop user','@', @ @ hostname,') +-- + | concat ('drop user''@',', @ @ hostname '') | +-- + | drop user'@ 'bei-f0q5bc2.gemalto.com' | +-- -+ 1 row in set (0.00 sec)

Supplementary extension: mysql insert string with single quotation marks + double quotation marks

As follows:

String needInsertStr= single and double quotation mark string; String finalStr = needInsertStr.replaceAll ("'", "\"); / / function equals to the escape symbol before the single quotation mark\ / / for other special characters, long dateMillions = System.currentTimeMillis (); StringBuilder stringBuilder = new StringBuilder (); stringBuilder.append ("insert into"); stringBuilder.append (tableName); stringBuilder.append ("(uuid,name,metric_id,service_id,script,deleted,created,updated)") StringBuilder.append ("values (uuid (),'"); stringBuilder.append (name); stringBuilder.append ("','"); stringBuilder.append (name); stringBuilder.append (",'"); stringBuilder.append (serviceId); stringBuilder.append (","); stringBuilder.append (finalStr); stringBuilder.append (", 0,"); stringBuilder.append (dateMillions); stringBuilder.append (","); stringBuilder.append (dateMillions); stringBuilder.append (")"); return stringBuilder.toString () / / the script field is a string with single and double quotation marks

Insert into table (string) values ('"this is a test statement" +\ 'Test single and double quotes\')

The above article uses multiple single quotation marks in MySQL concat, and the problem of three quotation marks is all the content that the editor has shared with you. I hope I can give you a reference and support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 283

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report