Ashwani Tiwari Ashwani Tiwari - 3 months ago 7
MySQL Question

Query which Find string and increment the count

I have table like that,

id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5

I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.


A simple update query required:

If you want to increase count only if the input parameter exactly matches the name then use this:

UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?

And if you want to increase count if the input parameter is a substring of name then you can use LIKE

UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')

Note: Replace the question mark (?) by your input parameter.