select.. group... subselect.. whatever? Please Help..!
| From: | Koo | Date: | Sat, 05 Aug 2000 18:59:24 +0000 |
| Subject: | select.. group... subselect.. whatever? Please Help..! | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-10266@lists.php.net to get a copy of this message | ||
Hello, I have a problem about SQL query statement...
This is my current SQL output:
mysql> select distinct items.itemID,words.word,types.type from
types,items,words,words_items
-> where words.wordID=words_items.wordID
-> and words.word like 'a%'
-> and types.typeID=items.typeID
-> and items.itemID=words_items.itemID
-> ;
+--------+---------+----------------+
| itemID | word | type |
+--------+---------+----------------+
| 1 | about | Novel |
| 1 | alike | Novel |
| 1 | almost | Novel |
| 1 | asad | Novel |
| 2 | asad | Novel |
| 1 | asadf | Novel |
| 1 | asdffsa | Novel |
| 2 | asdffsa | Novel |
| 1 | a7jlasf | Novel |
| 3 | alike | Joke |
| 3 | asad | Joke |
| 3 | asdffsa | Joke |
| 3 | a908as | Joke |
| 3 | ajkl8a | Joke |
| 4 | alike | Short Messages |
| 4 | asdffsa | Short Messages |
| 4 | a908as | Short Messages |
+--------+---------+----------------+
17 rows in set (0.01 sec)
PROBLEM: How do I query the database so that I can get the number
of non-duplicated ITEMs in each TYPE that contains a word
beginning with character "a".
This is the result that I expected (row order not important):
+----------------+----+
| type | no |
+----------------+----+
| Joke | 1 |(Only ONE item, i.e. itemID=3)
| Novel | 2 |(TWO items, i.e. itemID=1 and itemID=2)
| Short Messages | 1 |(Only ONE item, i.e. itemID=4)
+----------------+----+
I'VE DONE THE FOLLOWING, BUT IT DOESN'T OUTPUT THE RESULT I WANTED:
mysql> SELECT distinct types.type,count(*) as no
-> FROM types, words, words_items, items
-> WHERE words.word like 'a%'
-> and words.wordID=words_items.wordID
-> AND types.typeID=items.typeID
-> and items.itemID=words_items.itemID
-> GROUP BY type;
+----------------+----+
| type | no |
+----------------+----+
| Joke | 5 |(5[itemID=3])
| Novel | 9 |(7[itemID=1]+2[itemID=2])
| Short Messages | 3 |(3[itemID=4])
+----------------+----+
It simply reports the total instances of items that match the
criteria...
Hope that further explains what I want...
Sorry for the troubles. I've spent almost 2 days on thinking
and trying different combinations of query statement but in
vain. I'm not an SQL expert after all.
Thank you very much.
Regards,
Koo