where /和SQL子句跨三个表

时间:2013-06-03 06:03:11

标签: mysql join many-to-many

为什么当我将and添加到此查询以按标记字符串过滤时,我得不到任何结果。我如何能 包括这个:

`subject_tags`.`string_en` =  '%beach%'" 

正确?

SELECT *, ( 3959 * acos( cos( radians(22.2863943) ) * cos( radians( lat ) ) * cos( radians( subject.long ) - radians(114.1491375) ) + sin( radians(22.2863943) ) * sin( radians( lat ) ) ) )
AS distance 
FROM subject 
JOIN `subject_to_tag` ON `subject_to_tag`.`subject_id` = `subject`.`id`
JOIN `subject_tags` ON `subject_tags`.`id` = `subject_to_tag`.`tag_id`  
and `subject_tags`.`string_en` =  '%beach%'     
LIMIT 10

这是表结构....

enter image description here

感谢

2 个答案:

答案 0 :(得分:2)

有关

and `subject_tags`.`string_en` =  '%beach%'

我认为你的意思是

and `subject_tags`.`string_en` LIKE  '%beach%'

答案 1 :(得分:1)

如果你想匹配模式,那么你可以使用“LIKE”操作符作为Jim Garrison的回答。

and `subject_tags`.`string_en` LIKE  '%beach%'

但是如果你想要完全用于列的“海滩”一词,那么你应该是这样的:

and `subject_tags`.`string_en` =  'beach'.

另外,您可以查看Comparison Functions and Operators