需要 mysql 查询帮助

发布于 2024-08-23 14:27:13 字数 971 浏览 6 评论 0原文

我是 mysql 新手,所以请你帮助我。 我有 2 个表“单词”和“文本”

单词具有列:

  1. 单词
  2. 同义词

文本具有列:

  1. 文本
  2. 单词
  3. article_id

我需要获取唯一的words.word 和最大的唯一text.atricle_id 字段。同一个article_id可以有不同的单词。例如

words 
word     synonyms
 ----------------------- 
 car      auto, vehicle 
 water    syn1, syn2      
 bus      syn1, syn2

 text 
 text          word        article_id
 --------------------------------------- 
 any text      car            1
 any_text      water          1
 any_text      water          2
 any_text      car            2
 any_text      bus            1
 any_text      bus            3

 I need to get the result: 
 car   | 2
 water | 1
 bus   | 3

我有一个查询,但它返回非唯一的article_id

SELECT words.word, text.article_id 
FROM `words` , `text`
WHERE text.word = words.word
GROUP BY words.word
ORDER BY text.article_id DESC

I'm new to mysql so please can you help me.
I have 2 tables "words" and "text"

words has columns:

  1. word
  2. synonyms

text has columns:

  1. text
  2. word
  3. article_id

I need to get unique words.word, and biggest unique text.atricle_id fields. The same article_id can have different words. for example

words 
word     synonyms
 ----------------------- 
 car      auto, vehicle 
 water    syn1, syn2      
 bus      syn1, syn2

 text 
 text          word        article_id
 --------------------------------------- 
 any text      car            1
 any_text      water          1
 any_text      water          2
 any_text      car            2
 any_text      bus            1
 any_text      bus            3

 I need to get the result: 
 car   | 2
 water | 1
 bus   | 3

I have a query but it return non unique article_id

SELECT words.word, text.article_id 
FROM `words` , `text`
WHERE text.word = words.word
GROUP BY words.word
ORDER BY text.article_id DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-08-30 14:27:13

此查询将得到您想要的结果:

SELECT words.word, max(text.article_id) as biggest_article_id
FROM `words` , `text`
WHERE text.word = words.word
GROUP BY words.word
ORDER BY text.article_id DESC

结果:

 word  _ biggest_article_id
 bus   | 3
 car   | 2
 water | 2

注释 1:如您在问题中所述,水的 Maximum_article_id = 2 而不是 = 1。

注意 2:ORDER BY text.article_id DESC 不会按照您在问题中陈述的顺序给出结果。

This query will get the result you want:

SELECT words.word, max(text.article_id) as biggest_article_id
FROM `words` , `text`
WHERE text.word = words.word
GROUP BY words.word
ORDER BY text.article_id DESC

Result:

 word  _ biggest_article_id
 bus   | 3
 car   | 2
 water | 2

Note 1: water has biggest_article_id = 2 and not = 1 as you state in the question.

Note 2: ORDER BY text.article_id DESC won't give you the result in the order you state in the question.

清旖 2024-08-30 14:27:13

也许这会做到这一点:

SELECT DISTINCT words.word, text.article_id 
FROM `words` , `text`
WHERE text.word = words.word
HAVING max(text.article_id)
GROUP BY words.word
ORDER BY text.article_id DESC

Maybe this will do it:

SELECT DISTINCT words.word, text.article_id 
FROM `words` , `text`
WHERE text.word = words.word
HAVING max(text.article_id)
GROUP BY words.word
ORDER BY text.article_id DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文