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

Mini Program to Implement the Complete Shopping Cart

The mini program implements a complete shopping c...

MySQL data archiving tool mysql_archiver detailed explanation

Table of contents I. Overview 2. pt-archiver main...

Sample code for configuring nginx to support https

1. Introduction Are you still leaving your websit...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

Three ways to check whether a port is open in a remote Linux system

This is a very important topic, not only for Linu...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

Docker image loading principle

Table of contents Docker images What is a mirror?...

Summary of common tool examples in MySQL (recommended)

Preface This article mainly introduces the releva...

Web Design: Web Music Implementation Techniques

<br />When inserting music into a web page, ...

Complete steps for mounting a new data disk in CentOS7

Preface I just bought a new VPS. The data disk of...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

Website Building Tutorial for Beginners: Learn to Build a Website in Ten Days

The 10-day tutorial uses the most understandable ...