简体   繁体   中英

Mysql select column with concat

i want some correction here. i want to select all people with name fred in database

Here's my query:

SELECT * FROM tdble WHERE CONCAT(name) LIKE CONCAT('%', REPLACE('fred', '')'%')

What you are asking can be simply achieved by either using the "=" operator of the wildcard operator "like" statement.

If you wish to find all records that have an exact match to the name 'Fred' then you should model your query as so:

Select * From tdble Where Name = 'fred'

However, if you want to get all results where the names have 'fred' included in it somewhere use the wildcard operator.

Select * From tdble Where Name like '%fred%'

Also you can further model your query to know where exactly in which form you want 'fred' to appear. Example if you want 'Fred' to be as the last characters of your name string, for instance you wish to get names which ends with fred then model your query like this:

Select * From tdble Where Name like '%fred'

(you will get results like 'alfred', provided there is an alfred in your table)

However if you wish to get all names that begin with fred, model the query like this:

Select * From tdble Where Name like 'fred%'

(you will get results like 'fredinane', provided there is a fredinane in your table)

Cheers

  • If you want to fetch record with name 'fred', you can simply do Select * from TableName Where Name = 'fred' .
  • If you want to fetch records which their names' string contain 'fred', you have to use select * from TableName where Name like '%fred%'

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