The indexOf-like method LOCATE () and indexoflocate in mysql

Source: Internet
Author: User

The indexOf-like method LOCATE () and indexoflocate in mysql

 

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

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

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

 

Question: there are multiple subjects. One subject has multiple answers, and each question has four answers (ABCD). The number of ABCD choices is counted.

SELECT
Sum (
CASE
WHEN (
LOCATE ('A', option name)> 0
) THEN 1 ELSE
0
END
) As,
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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.