关键字搜索,多表,PHP和Mysql,使用哪种连接?

关键字搜索,多表,PHP和Mysql,使用哪种连接?,php,mysql,search,join,Php,Mysql,Search,Join,我有三张表:事件、位置、事件位置。事件可以有多个位置。 位置表包含纬度、经度和地理编码字段 “你好,世界”的关键词也是如此 对事件表的简单查询变成 Select * from event where keywords like '%hello%' OR keywords like '%world%' 但如果用户已经输入了他们的位置,那么我想在这个查询中也包括位置表,这样用户就可以指定他们选择的位置,我该怎么做呢 因此,基本上有3种类型的搜索查询 只是关键词 关键词与定位 关键词、邻近性和位置

我有三张表:事件、位置、事件位置。事件可以有多个位置。 位置表包含纬度、经度和地理编码字段

“你好,世界”的关键词也是如此

对事件表的简单查询变成

Select * from event where keywords like '%hello%' OR keywords like '%world%'
但如果用户已经输入了他们的位置,那么我想在这个查询中也包括位置表,这样用户就可以指定他们选择的位置,我该怎么做呢

因此,基本上有3种类型的搜索查询

  • 只是关键词

  • 关键词与定位

  • 关键词、邻近性和位置
我试过这样的东西-

   select * from event where keywords like '%hello%' OR
   keywords like '%world%' INNER JOIN location ON 
   location.location_id = event_location.location_id 
   INNER JOIN 
   event_location ON event_location.event_id = event.event_id
内部联接意味着事件必须有一个或多个位置。如果事件没有得到任何位置,它将不会出现在搜索结果中。所以请帮帮我,我该怎么做

感谢您的帮助。

使用s(我更改了联接的顺序,并修复了where子句位置,该位置放错了位置)

这样,您将为没有位置的事件获取空值


另外,尽量不要使用select*,而要命名您感兴趣的列。

您的连接语法已经被弄乱了。在任何情况下,如果内部联接不切割它,请使用外部联接。;-)

此外,使用表别名从来都不是一个坏主意,尤其是当您的表名很长时

select * from event LEFT JOIN event_location ON 
event.event_id = event_location.event_id 
LEFT JOIN 
location ON event_location.location_id = location.location_id
where keywords like '%hello%' OR
keywords like '%world%' 
SELECT
  *
FROM
  event AS e
  LEFT JOIN event_location AS el ON el.event_id = e.event_id
  LEFT JOIN location       AS  l ON l.location_id = el.location_id
WHERE
  e.keywords LIKE '%hello%' 
  OR e.keywords LIKE '%world%'