MySQL text search example
I have a table called table1 with 3 columns, a, b, and c.
b
is varchar(600), say, and I wish to return the int from column a
, say.
Let's assume the term I am looking for is "This term".
What would a single complete example be with the basic syntax required to search for this phrase as above in b
and return the respective a
in MySQL as the Internet is totally unclear on this?
Basically, what would the mysql_query string look like for a text search?
$result = mysql_query("SELECT a FROM sometable WHERE b LIKE '%This term%'");
Depending on what you plan to do, MySQL Fulltext Indices might be interesting for you.
The following will look for 'This term' anywhere in b
SELECT a FROM table1 WHERE b LIKE('%This term%')
You could also just find records that start with 'This term'
SELECT a FROM table1 WHERE b LIKE('This term%')
The % operator is the wildcard in the like condition.
SELECT * FROM table1
WHERE b LIKE '%This term%'
精彩评论