【问题标题】:How to limit words but not definitions using mysql如何使用mysql限制单词而不是定义
【发布时间】:2017-08-24 03:54:51
【问题描述】:

我有两个相关的表格,分别称为 wordsdefinitions。每个表有超过 250,000 行。这是我的表的设置方式。

单词

id word
 1 book
 2 love
 3 hate
 4 smile
 and it goes on.

定义

id wordid pos definition
 1 1      v   to enter in a book or list; record; register.
 2 1      n   a book, a bible
 3 2      n   noun definition of love
 4 2      v   verb definition of love
 and it goes on.

我写了一个 php,从单词中选择 5 个结果,使用:
$query = "SELECT id, word FROM words LIMIT 5";

然后我循环 5 个结果中的每一个,例如

foreach($results as $row){
   $wordid = $row['id'];
   $word = $row['word'];

   // I really dont want to do this way
   $definitions_query = "SELECT * FROM definition WHERE wordid = $wordid";

   // After the query
   foreach($definitions_result as $row1){
       $definition = $row1['definition'];
       $pos        = $row1['pos'];

     // Desired Results
     $data[$word][$pos][] = $definition;
   }
}

我的代码完成了任务,但是太慢了。

所以,我加入了"SELECT * FROM words JOIN definition ON words.id = wordid LIMIT 5" 之类的表格。

现在,查询只提取 5 个结果,我无法将单词限制为 5 行。

我的目标是以非常快速的方式提取 5 个单词及其许多定义。请记住我想要的结果数组格式。我该怎么办?非常感谢您的帮助。

【问题讨论】:

  • 先只拉出五个单词,然后与定义表进行连接。
  • @kkmishra 你介意输入查询吗?

标签: php mysql arrays


【解决方案1】:

试试这个查询。它将限制父数据并加入表

SELECT w.word,d.definition,d.pos FROM 
  (SELECT id, word FROM words ORDER BY id LIMIT 5) w 
  JOIN definition  d ON d.wordid = w.id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2015-11-25
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多