简体   繁体   中英

How to search string field in MySQL which contain two or more words separated by space?

I have some old DB where I did not put some restrictions for inserting names through web PHP form. Now I have put them there to be only single word not two words or more. There are 10 000 rows now after the years in that DB.

Example field in DB: "John Doe Blue" With new rules user can only fill in "JohnDoeBlue"

Is there any way how to Select all cases where I have names which do not contain only single word but multiple in that string field? My new function which prevents wrong or multiple words in the field is this:

  public function wrongName($name)
  {
    return !preg_match("/^[a-zA-Z0-9]{3,20}$/", $name);
  }

How can I select from DB all cases which do not meet this function but are already existing in the DB?

If you want to remove any spaces from a column containing data like this "John Doe Blue" all you need to do is

UPDATE table SET column = REPLACE(column, ' ','');

If you want to check that you are only getting the rows you want before doing the update try

SELECT column, REPLACE(column, ' ','') as potentialNewColumn
FROM table 
WHERE INSTR(column, ' ') > 0 ;

以下查询可用于替换和搜索。

select * from table where REPLACE (column,' ','') like '%your value%':

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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