简体   繁体   English

如何使用PHP从MySQL整理垂直线

[英]How I can CONCAT vertical lines from MySQL using PHP

If have following problem. 如果有以下问题。 I cannot get values from MySQL when I have them separated with | |分隔为|时,无法从MySQL获取值|

I have following string: 我有以下字符串:

SELECT * FROM user WHERE CONCAT(',', category, ',') REGEXP ',(lc_8|swimming),'

and this string only works when in my MySQL I have column category with value lc_8,swimming, 并且此字符串仅在我的MySQL category具有值为lc_8,swimming,category lc_8,swimming,

How I can change string above to get same values but if in column I have lc_8|swimming| 我如何更改上面的字符串以获得相同的值,但是如果在列中我有lc_8|swimming| ?

| is a special character in regexps. 是正则表达式中的特殊字符。 You would need to escape it, like \\| 您需要将其转义,例如\\| . Try: 尝试:

SELECT * FROM user WHERE CONCAT(',', category, ',') REGEXP ',(lc_8\|swimming),'

Characters that need escaping in regexes are as follows: 需要在正则表达式中转义的字符如下:

* ? + [ ( ) { } ^ $ | \

If you really want to match the parentheses in your example, you would need to quote them as well. 如果您确实想匹配示例中的括号,则也需要引用它们。

( and | have a special meaning in a regular expression, you need to escape them. (|在正则表达式中有特殊含义,您需要对它们进行转义。

SELECT *
FROM user
WHERE CONCAT(',', REPLACE(category, '|', '\\|'), ',') REGEXP ',\(lc_8\|swimming\),'

But if you're searching a comma-delimited list, it would be better to use the function designed for that: 但是,如果要搜索以逗号分隔的列表,则最好使用为此设计的函数:

SELECT * FROM user
WHERE FIND_IN_SET('(lc_8|swimming)', category)

Even better would be to not store comma-delimited lists in the first place, but put the categories in a separate table with one row for each category. 最好不要先存储以逗号分隔的列表,而是将类别放在单独的表中,每个类别一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM