简体   繁体   English

根据MySQL中另一个表中的值从表中选择

[英]Select from table based on values in another table in MySQL

I have two simple tables in a database 我在数据库中有两个简单的表

papers 文件

ID Title                              Author 
1  A study of turtles                 Mary
2  Dietary habits of ducks            Kate
3  Similarities of turtles and cats   Fred

keywords 关键字

ID Keyword                            
1  turtles                 
2  ducks          
3  turtles
3  cats

I would like to select all the papers that have the keyword "turtles". 我想选择所有包含关键字“ turtles”的论文。 That is, it would return 也就是说,它将返回

ID Title                              Author 
1  A study of turtles                 Mary
3  Similarities of turtles and cats   Fred

or 要么

ID Title                              Author   Keyword
1  A study of turtles                 Mary     turtles
3  Similarities of turtles and cats   Fred     turtles

I'm not concerned if the Keyword title is or is not included. 我不关心关键字标题是否包含在内。

I think I need to use a select and inner join using an alias - but am not getting the syntax right. 我想我需要使用使用别名的选择和内部联接-但语法不正确。

I have tried 我努力了

select * from (select papers.*, keywords.* from papers inner join keywords on papers.id = keywords.id) where keyword = "turtles";

which gives the error Every derived table must have its own alias 给出错误Every derived table must have its own alias

I've tried to follow What is the error "Every derived table must have its own alias" in MySQL? 我尝试遵循MySQL中的“每个派生表必须具有自己的别名”错误是什么?

select * from (select papers.*, keywords.* from 
    (papers inner join keywords on papers.id = keywords.id) as T) 
    as T) 
    where keyword = "turtles";

which returns a syntax error. 返回语法错误。

I know there are many similar enquiries, but I am new to MySQL and am getting confused with the questions / examples. 我知道有很多类似的查询,但是我是MySQL的新手,并且对问题/示例感到困惑。

EDIT: Clarifying that I am wanting to return the IDs that match the keyword "turtle" from the table keyword (1 and 3 in this example), then select from the papers table the rows corresponding to these IDs. 编辑:澄清我想从表关键字(此示例中为1和3)返回与关键字“ turtle”匹配的ID,然后从papers表中选择与这些ID对应的行。 I am not looking to find the keywords in the title. 我不想在标题中找到关键字。

We can try searching, for each paper title, the entire set of keywords, looking for a match. 我们可以尝试为每个论文标题搜索整个关键字集,以寻找匹配项。 The REGEXP operator is helpful here. REGEXP运算符在这里很有帮助。 In particular, we are looking to match \\bkeyword\\b against anywhere in the paper title. 特别是,我们希望将\\bkeyword\\b与论文标题中的任何地方匹配。

SELECT p.*
FROM papers p
WHERE EXISTS (SELECT 1 FROM keywords k
              WHERE p.title REGEXP CONCAT('[[:<:]]', k.keyword, '[[:>:]]'));

Demo 演示版

Edit: 编辑:

If you just want to search the paper titles against a single known keyword, eg turtles , then use this simplification: 如果您只想针对一个已知关键字(例如turtles搜索论文标题,请使用以下简化方法:

SELECT *
FROM papers
WHERE title REGEXP '[[:<:]]turtles[[:>:]]';

Simple join (inner): 简单加入(内部):

SELECT *
FROM keywords k
JOIN papers p USING (id)
WHERE k.keyword='turtles'

I really like Tim's answer 我真的很喜欢蒂姆的答案

Here's my own attempt 这是我自己的尝试

SELECT
  *
 FROM
   papers p
   INNER JOIN keywords k ON p.`Title`  LIKE CONCAT('%', k.keyword ,'%')
  ;

SQLFiddle SQLFiddle

As Tim commented on this answer, it'll bring substrings as well a full word match In order to resolve this, there a need to add spaces to the % parameters 正如Tim在此答案中所评论的那样,它将带来子字符串以及完整的单词匹配。为解决此问题,需要在%参数中添加空格

SELECT
  *
 FROM
   papers p
   INNER JOIN keywords k ON p.`Title`  LIKE CONCAT('% ', k.keyword ,' %')
  ;

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

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