ASR
ASR

Reputation: 3549

mysql search query for 2 columns with single parameter

I am new to databases. In mysql database I have one table course. My question is: how to search all related words in both columns course_name and course_description and i need to get all the matched words in both columns? Can any one tell me the sql query for it? I have tried to write a query, but I am getting some syntax errors.

+----------+-----------+-----------------+------------+------------+
| courseId | cname     | cdesc           | sdate      | edate      |
+----------+-----------+-----------------+------------+------------+
|      301 | physics   | science         | 2013-01-03 | 2013-01-06 |
|      303 | chemistry | science         | 2013-01-09 | 2013-01-09 |
|      402 | afm       | finanace        | 2013-01-18 | 2013-01-25 |
|      403 | English   |  language       | 2013-01-17 | 2013-01-24 |
|      404 | Telugu    | spoken language | 2013-01-10 | 2013-01-22 |
+----------+-----------+-----------------+------------+------------+

Upvotes: 0

Views: 448

Answers (3)

Devang Rathod
Devang Rathod

Reputation: 6736

If you want to search exact word

SELECT * FROM course WHERE cname ='word' AND cdesc = 'word'

OR you can also find each value and not just start from begining.

SELECT * FROM course WHERE cname = '".%searchtermhere%."' AND cdesc = '".%searchtermhere%."'

Upvotes: 0

DB_learner
DB_learner

Reputation: 1026

Since you say single parameter i guess. You will get either 'science' as input or 'physics' as input. Then you could simply use 'OR'.

select * from course where cname = (Input) or cdesc = (Input)

Upvotes: 0

Michael Bellamy
Michael Bellamy

Reputation: 541

SELECT * from course WHERE cname='%searchtermhere%' AND cdesc='%searchtermhere%'

Adding the percent % makes the search within each value and not just beginning with.

Upvotes: 2

Related Questions