如何从另一张表中获取句子中第一个词的ID?

问题描述 投票:0回答:1

我有一个表格,其中包含如下句子

enter image description here

和另一张基于此表生成的表格,其中包含句子使用的所有单词及其频率,如图所示。

enter image description here

我想在第一张表中增加一列,命名为Score,列名为该句子中使用最少的单词的WID,我的预期输出是这样的。

enter image description here

它应该总是从第二个表中选择第一个准确度最低的单词。

有没有一种方法可以做到这一点,使用Phpmyadmin和一些MySQL查询?

create table messages(sent varchar(200), verif int, wid);
insert into messages values
    ('hello my name is alex', null),
    ('hey alin and alex I''m tom', null, null),
    ('hello alex my name is alin', null, null);

生成第二个表

create view words_view as with recursive cte as (
    select 
        substring(concat(sent, ' '), 1, locate(' ', sent)) word,
        substring(concat(sent, ' '), locate(' ', sent) + 1) sent
    from messages
    union all
    select 
        substring(sent, 1, locate(' ', sent)) word,
        substring(sent, locate(' ', sent) + 1) sent
    from cte
    where locate(' ', sent) > 0
)
select row_number() over(order by count(*) desc, word) wid, word, count(*) freq
from cte 
group by word
order by wid

现在,我需要更新第一张表中的wid,用第二张表中该句子中使用频率最低的词的wid。

mysql sql string phpmyadmin recursive-query
1个回答
1
投票
SELECT s.sentence
     , w.id 
  FROM words w 
  JOIN sentences s ON SUBSTRING_INDEX(s.sentence,' ',1) = w.word;
+----------------------------+----+
| sentence                   | id |
+----------------------------+----+
| hello my name is alex      |  3 |
| hello alex my name is alin |  3 |
| hey alin and alex I'm tom  |  8 |
+----------------------------+----+
© www.soinside.com 2019 - 2024. All rights reserved.