简体   繁体   中英

Combine multiple column values in to single value in SQL select statement

I have an autocomplete field where user can enter lastname firstname middlename in the same order.

Using lastname firstname middlename (as a term) I have to show the autocomplete dropdown.

On my database I have 3 columns for firstname lastname and middlename. Now I have to compare 3 column values (asc_lastname, asc_firstname, asc_middlename) of same row with user input (term).

Here is the SQL query. Please correct my mistake.

$rs = mysql_query( "select asc_lastname, asc_firstname, asc_middlename from issio_asc_workers INNER JOIN issio_workers_asc_sc_list 
ON  issio_asc_workers.lname_fname_dob=issio_workers_asc_sc_list.lname_fname_dob where issio_workers_asc_sc_list.center_id='$cid' 
AND issio_workers_asc_sc_list.center_user_type = '31' AND issio_workers_asc_sc_list.center_status <> 'deleted' AND 
(issio_asc_workers.asc_lastname+' '+issio_asc_workers.asc_firstname+' '+issio_asc_workers.asc_middlename) 
LIKE '". mysql_real_escape_string($term) ."%'  ORDER BY issio_asc_workers.lname_fname_dob ASC LIMIT 0,10");

Is it possible to compare 3 column values at a time using a SQL query like

 select * 
 from table 
 where (column1+''+column2+''column3) like 'this is good'

Sorry for my poor sentences.

Try this query:

SELECT * FROM your_table WHERE CONCAT(column1, ' ', column2, ' ', column3) LIKE '%this is good%'

Documentation for CONCAT

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