简体   繁体   中英

Searching SQL database and CONCAT data

New to php and sql so i will try to explain:

I have a SEARCH field in PHP and i am trying to search by 'ProposalName' that match with what the user enters.

This prints out fine:

SELECT
    rec_proposal.ProposalID,
    ProposalName,
    Status,
    researcher.FirstName,
    researcher.LastName,
    reviewer.FirstName as revFirstName,
    reviewer.LastName as revLastName,
    reviewer.UserID as revUserID,
    review.ReviewDate as revDate, 
    rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
    ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
    ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
    ON review.UserID=reviewer.UserID

But now using all the columns I need the above code to do something like this

  SELECT * FROM rec_proposal WHERE CONCAT (ProposalName) LIKE'%test%'

SO if user enters the word 'test' you would see ProposalName that contains the words test

Just add your WHERE clause, it should work. And as scaisEdge noted in their comment, you don't need CONCAT() if you are just evaluating a single column :

SELECT
    rec_proposal.ProposalID,
    ProposalName,
    Status,
    researcher.FirstName,
    researcher.LastName,
    reviewer.FirstName as revFirstName,
    reviewer.LastName as revLastName,
    reviewer.UserID as revUserID,
    review.ReviewDate as revDate, 
    rec_proposal.DateSubmitted
FROM rec_proposal
INNER JOIN User AS researcher
    ON rec_proposal.userid = researcher.UserID
LEFT JOIN review
    ON rec_proposal.ProposalID=review.ProposalID
LEFT JOIN User as reviewer
    ON review.UserID=reviewer.UserID
WHERE rec_proposal.ProposalName LIKE '%test%'

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