MySQL REGEXP - 选择某些数字和字符模式

时间:2016-06-15 00:31:07

标签: mysql regex

任何人都知道如何在最后用1选择某种数字模式?

实施例

SELECT pattern FROM table WHERE pattern REGEXP '1_2+2_2+3_2+4_2&2016-06-09&1';

SELECT pattern FROM table WHERE pattern REGEXP '2_1&2016-06-09&1';

使用相同的数字 - 下划线数字,&符号,日期,&符号,数字;只要数字1在最后?

编辑:

实际上,让我更好地说出来。如何使用REGEXP选择&符号和字符串末尾的数字1?

2 个答案:

答案 0 :(得分:3)

您不需要正则表达式。只需使用LIKE

LIKE '%&1'

%使其不会锚定到字符串的开头。 LIKE不是正则表达式,而是更接近于glob语法。它也可能比正则表达更快。

答案 1 :(得分:1)

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

<!---

    <!--- test data --->
    <cfset query = queryNew("type_name,count", "VARCHAR,INTEGER")>
    <cfset queryAddRow(query, 3)>
    <cfset querySetCell(query, "type_name", "test1", 1)>
    <cfset querySetCell(query, "count", "5", 1)>
    <cfset querySetCell(query, "type_name", "test2", 2)>
    <cfset querySetCell(query, "count", "4", 2)>
    <cfset querySetCell(query, "type_name", "test4", 3)>
    <cfset querySetCell(query, "count", "7", 3)>

--->

<!--- number of tests to list --->
<cfset expectedNumberOfRows = 4>

<!--- remember all columns while transforming query to array of structs --->
<cfset queryColumns = listToArray(query.columnList)>

<!--- initialize array of structs --->
<cfset testResults = arrayNew(1)>
<cfloop from="1" to="#expectedNumberOfRows#" index="i">

    <cfset blankResult = structNew()>

    <!--- initialize all columns for each row --->
    <cfloop array="#queryColumns#" index="columnName">
        <cfset blankResult[columnName] = "">
    </cfloop>

    <!--- default values for specific columns --->
    <cfset blankResult["type_name"] = "test#i#">
    <cfset blankResult["count"]     = "0">

    <cfset testResults.add(blankResult)>
</cfloop>

<!--- transfer cell values from each query row to array of structs --->
<cfset queryRowIndex = 1>
<cfloop query="query">

    <!--- extract possible row index --->
    <cfset testNumber = trim( reReplace(query.type_name, "[^0-9]*([0-9]+)$", "\1") )>

    <!--- transfer cells --->
    <cfif reFind("^[0-9]+$", testNumber)>
        <cfloop array="#queryColumns#" index="columnName">
             <cfset testResults[int(testNumber)][columnName] = query[columnName][queryRowIndex]>
        </cfloop>
    </cfif>

    <cfset queryRowIndex++>
</cfloop>

<cfoutput>

    <table>
        <cfloop array="#testResults#" index="testResult">
            <tr>
                <td>#testResult.type_name#</td>
                <td>#testResult.count#</td>
                <!--- add additional columns if desired --->
            </tr>
        </cfloop>
    </table>

</cfoutput>

注意: 您也可以使用LIKE运算符从头开始搜索。

Here is an Example.

SELECT column_name
FROM table 
WHERE column_name LIKE '%&1';