views:

63

answers:

2

hi all, 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
A: 

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
duffymo
+1  A: 

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.

Leniel Macaferi
I need the biggest unique article_id so this query is not right for me.
bera
As I understood your question, you want to get the biggest article id associated with a word. The query I gave you will do just that. You're grouping words and for each one of them you're getting the biggest article id associated with a given word.
Leniel Macaferi
One thing that I didn't understand: if you want to get the biggest article id, why the word 'water' has an article id = 1 in the result you've shown? See that in your text table, you have a row for 'water' that have an article id = 2 so that this is the biggest article id associated with the word 'water'.
Leniel Macaferi
yes article_id 2 is the biggest for the word water but i need the biggest unique, so the biggest unique article_id for the word water is 1, becouse the article_id 2 has the word "car", which goes before the "water" word
bera
Articles must be UNIQUE for every word.
bera