我是mysql的新手,所以请你能帮助我。 我有2个表“单词”和“文本”
单词有列:
文字有列:
我需要获得独特的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
答案 0 :(得分:1)
此查询将获得您想要的结果:
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:正如您在问题中所述,水的big_article_id = 2而不是= 1。
注2:ORDER BY text.article_id DESC
不会按照您在问题中声明的顺序给出结果。
答案 1 :(得分:0)
也许会这样做:
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