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

Detailed explanation of type protection in TypeScript

Table of contents Overview Type Assertions in syn...

We're driving IE6 to extinction on our own

In fact, we wonder every day when IE6 will really...

Example of using Vue built-in component keep-alive

Table of contents 1. Usage of keep-alive Example ...

How to smoothly upgrade and rollback Nginx version in 1 minute

Today, let's talk about a situation that is o...

How to configure Nginx to support ipv6 under Linux system

1. Check whether the existing nginx supports ipv6...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

A complete guide to Linux environment variable configuration

Linux environment variable configuration When cus...

Specific method of viewing user authorization information in mysql

Specific method: 1. Open Command Prompt 2. Enter ...

Summary of Vue's monitoring of keyboard events

Key Modifiers When listening for keyboard events,...

HTML+CSS merge table border sample code

When we add borders to table and td tags, double ...

This article helps you understand PReact10.5.13 source code

Table of contents render.js part create-context.j...