简体   繁体   English

如何在单独的mysql表中搜索字词?

[英]How do I search for term in separate mysql tables?

I'd like to search multiple columns and multiple tables. 我想搜索多个列和多个表。

I imported all my tables into sqlfiddle here: http://www.sqlfiddle.com/#!2/60689e/3 我在这里将所有表导入sqlfiddle: http ://www.sqlfiddle.com/#!2/60689e /3

What I'd like to select is user id, name_surname, avatar, from users table Based on keyword, matching column "location" or "name_surname" in users table. 我想user id, name_surname, avatar, from users table选择user id, name_surname, avatar, from users table基于关键字,匹配user id, name_surname, avatar, from users table “ location”或“ name_surname”列。 This is relatively simple: 这是相对简单的:

SELECT u.name_surname,
       u.avatar,
       u.location
FROM users u
WHERE u.location LIKE :kwd
OR u.name_surname LIKE :kwd

The tricky part for me is querying table for matches in words_en table, which essentially should be queried against connections table where I actually can see which user is associated with which word... How do I do this last part escapes me. 对我来说,最棘手的部分是查询表中是否有words_en表中的匹配 ,该表本质上应该在连接表中查询,在该表中我实际上可以看到哪个用户与哪个词相关联...我该怎么做最后一部分逃脱了。

So I probably need to: 所以我可能需要:

  1. query words_en and find out if there is a match at all. 查询words_en,找出是否完全匹配。
  2. if there is a match I need to look at id of word_en word and see if this id exists in connections table. 如果有匹配项,我需要查看word_en word的ID,并查看该ID是否存在于连接表中。
  3. Select distinct user_ids from connections table. 从连接表中选择不同的user_id。
  4. Select name_surname for the selected user_ids. 为所选的user_id选择name_surname。

Thanks for looking. 感谢您的光临。 S. S.

To get all users based on a search of the words_en table would look something like SQL Fiddle : 要基于对words_en表的搜索来获取所有用户,看起来像SQL Fiddle

SELECT u.id, u.name_surname, u.avatar
FROM users AS u
WHERE u.id IN 
(
  SELECT c.user_id
  FROM connections AS c 
    JOIN words_en AS w 
    ON w.id = c.word_id
  WHERE w.word = 'word1'
    OR w.word = 'word2'
) 
AND u.location LIKE '%Nunya%'
  OR u.name_surname LIKE '%sandro%'

If you want to use the same search term for any of the three fields then use the following: 如果要对三个字段中的任何一个使用相同的搜索词,请使用以下内容:

SELECT u.name_surname, u.avatar, u.location
FROM users AS u
  JOIN connections AS c ON c.user_id = u.id
  JOIN words_en AS w ON w.id = c.word_id
WHERE (w.word LIKE '%word1%'
  OR u.location LIKE '%word1%'
  OR u.name_surname LIKE '%word1%') 
  AND u.privacy = 3

I hope I did not misunderstand your problem. 希望我不要误会你的问题。 Correct me if I am wrong some where. 如果我在某些地方做错了,请纠正我。

SELECT u.name_surname,
       u.avatar,
       u.location
FROM USERS AS U
INNER JOIN CONNECTIONS AS C
ON  C.USER_ID = U.ID
INNER JOIN WORDS_EN AS W
ON  W.ID = C.WORD_ID
AND W.WORD LIKE 'word1%'

I have hard coded word1 for example, you can also make INNER JOIN of WORDS_EN as LEFT JOIN if you need all user information's having connections regardless of their words in WORDS_EN. 例如,我已对word1进行了硬编码,如果需要所有用户信息都具有连接能力,而不论WORDS_EN中的单词是什么,也可以将WORDS_EN INNER JOIN WORDS_ENLEFT JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM