Problems with using multiple single quotes and triple quotes in MySQL concat

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often use character concatenation. I don't understand the quotation marks for 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 to me in detail?)

SQL code

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

Normal sentence:

SQL code

SUM(CASE Leave WHEN '01' THEN DaysNo END) AS [CASE Leave],
SUM(CASE Leave WHEN '02' THEN DaysNo END) AS [Sick Leave]

Short answer:

Simply put, single quotes are used to concatenate statements, and three quotes are used to concatenate string variable values.

Because quotation marks have special meanings, they must be written twice to escape them.

2. Is there any difference between single quotes and double quotes for strings?

Simply put, if you are using it normally, use single quotes. If you are inside single quotes, you must use single quotes. Then you need to add an extra single quote (escape) to make it three single quotes.

In standard SQL, string literals are enclosed in single quotes.

If the string itself also includes single quotes, use two single quotes (note, not double quotes; double quotes in the string do not need to be escaped separately).

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

Brief summary:

When concat a string, if single quotes are required in the statement, two single quotes can be used 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 inserts a string with single quotes + double quotes

As shown below:

String needInsertStr= a string between single and double quotes;

String finalStr = needInsertStr.replaceAll("'", "\\\\\'"); //The effect is equivalent to adding an escape symbol before the single quote\
//Same 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 quotes

insert into table(string) values(' "This is a test statement" + \'test single and double quotes\' ')

The above article about using multiple single quotes and triple quotes in MySQL concat is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to insert a value containing single quotes or backslashes in MySQL statements
  • A detailed analysis of the murder caused by a misplaced double quote in MySQL
  • About Mysql query with single quotes and inserting strings with single quotes
  • Analysis of MYSQL performance issues caused by a single quote
  • Detailed explanation of the difference and usage of quotes and backticks in MySQL

<<:  Docker installs Elasticsearch7.6 cluster and sets password

>>:  Detailed explanation of Excel parsing and exporting based on Vue

Recommend

What is Software 404 and 404 Error and what is the difference between them

First of all, what is 404 and soft 404? 404: Simpl...

JavaScript+html implements random QR code verification on front-end pages

Share the cool front-end page random QR code veri...

Summary of MySQL password modification methods

Methods for changing passwords before MySQL 5.7: ...

How to count down the date using bash

Need to know how many days there are before an im...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Linux system (Centos6.5 and above) installation jdk tutorial analysis

Article Structure 1. Preparation 2. Install Java ...

Several techniques for playing sounds with CSS

CSS is the realm of style, layout, and presentati...

Summary of problems encountered in the implementation of Vue plug-ins

Table of contents Scene Introduction Plugin Imple...

Summary of uncommon js operation operators

Table of contents 2. Comma operator 3. JavaScript...

Detailed explanation of the use of this.$set in Vue

Table of contents Use of this.$set in Vue use Why...

JavaScript Reflection Learning Tips

Table of contents 1. Introduction 2. Interface 3....