mysql indexof function usage instructions

mysql indexof function usage instructions

As shown below:

LOCATE(substr,str)

Returns the first occurrence of substring substr in string str. If substr is not in str, returns 0.

mysql> select LOCATE('bar', 'foobarbar'); -> 4 mysql> select LOCATE('xbar', 'foobar'); -> 0

Supplement: LOCATE() method similar to indexOf in MySQL

LOCATE(substr, str), LOCATE(substr, str, pos)

The first syntax returns the position of the first occurrence of substr in string str.

The second syntax returns the position of the first occurrence of the string substr in the string str, starting at position pos. If substr is not in str, the return value is 0.

question:

There are multiple subjects, multiple choice questions under one subject, and four answers (ABCD) for each question. Count the number of ABCD choices for each question in each subject.

SELECT
 sum(
  CASE
  WHEN (LOCATE('A', option name) > 0) THEN
   1
  ELSE
   0
  END
 ) AS A,
 sum(
  CASE
  WHEN (LOCATE('B', option name) > 0) THEN
   1
  ELSE
   0
  END
 ) AS B,
 sum(
  CASE
  WHEN (LOCATE('C', option name) > 0) THEN
   1
  ELSE
   0
  END
 ) AS C,
 sum(
  CASE
  WHEN (LOCATE('D', option name) > 0) THEN
   1
  ELSE
   0
  END
 ) AS D
FROM
 Table name

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Some common mistakes with MySQL null
  • Use of MySQL SHOW STATUS statement
  • Tips on MySQL query cache
  • In-depth explanation of InnoDB locks in MySQL technology
  • MySQL master-slave synchronization, implementation principle of transaction rollback
  • Summary of several error logs about MySQL MHA setup and switching

<<:  Docker binding fixed IP/cross-host container mutual access operation

>>:  JavaScript Reflection Learning Tips

Recommend

JS implements circular progress bar drag and slide

This article example shares the specific code of ...

Analysis of the difference between Mysql InnoDB and MyISAM

MySQL supports many types of tables (i.e. storage...

Use of MySQL DDL statements

Preface The language classification of SQL mainly...

MySQL inspection script (must read)

As shown below: #!/usr/bin/env python3.5 import p...

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

Floating menu, can achieve up and down scrolling effect

The code can be further streamlined, but due to t...

Comparing Node.js and Deno

Table of contents Preface What is Deno? Compariso...

Vue implements dynamic circular percentage progress bar

Recently, when developing a small program, I enco...

How to obtain and use time in Linux system

There are two types of Linux system time. (1) Cal...

How to install Django in a virtual environment under Ubuntu

Perform the following operations in the Ubuntu co...

Installation and configuration method of Zabbix Agent on Linux platform

Here is a brief summary of the installation and c...

Why should you be careful with Nginx's add_header directive?

Preface As we all know, the nginx configuration f...

Why does MySQL paging become slower and slower when using limit?

Table of contents 1. Test experiment 2. Performan...