简体   繁体   中英

Mysql select where contains

I have a social networking site which I am in the process of constructing. Users can follow each other and it adds their name to a list of followers. I need to scan that list for the session username cookie to choose which statuses (posts) to display. In my sql recordset, I have the code:

SELECT *
FROM statuses
WHERE followedby CONTAINS '". $_SESSION['MM_Username']."' 

But this just brings up error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONTAINS 'Usernamecookie'' at line 1

Try this:

SELECT *
FROM statuses
WHERE followedby LIKE '%". $_SESSION['MM_Username']."%' 

LIKE clause is used for string matching.

Others have correctly answered this question already, but I just want to add the meaning of contains in MySQL:

MySQL only recognizes the CONTAINS SQL function when dealing with spatial data. It requires two graphics objects as arguments, and returns a 1 or 0 depending on if the first object completely contains the second. Designed as an implementation of the OpenGIS framework, the MySQL CONTAINS function does not work on ordinary strings, and will produce an error if you try it. MySQL only recognizes the LIKE and STRCMP functions when dealing with string of information.

So in your case, you want to use like .

References:

Try this:

mysql_query("
SELECT *
FROM statuses
WHERE followedby LIKE '%{$MM_Username}%' 
");

Put the MM_Username in its own var. Nicer code ;) Remember to enable index for FollowedBy if your table grows.

Read more about how to enable fulltext indexes here

而不是包含使用LIKE

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