Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql find only one hashtag not two

How to find in a MySQL database?:

# Some title 

(In a text field most of the titles are:
## Some title.
But there are some mistakes with only one #. How can I find them in a big database?. I know that they are only in the field text_post)

I could find that it could be something like:

SELECT * FROM blog
WHERE text_post LIKE '%#%'

But, how can I find when there is only one # and not two?

If I understand, it is not possible to use regex here. Is that right?

like image 937
segon Avatar asked May 06 '26 14:05

segon


2 Answers

Add an extract clause to eliminate the double ##.

SELECT * 
FROM blog
WHERE (text_post LIKE '%#%' AND text_post NOT LIKE '%##%')
like image 123
Matt Avatar answered May 09 '26 04:05

Matt


^    - Beginning of line
#    - hashtag
[^#] - Any character other than hashtag
|    - OR operator
$    - End of line

select  *
from    blog
where   text_post rlike '^#([^#]|$)'    
like image 40
David דודו Markovitz Avatar answered May 09 '26 04:05

David דודו Markovitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!