9 messages in com.mysql.lists.mysqlRe: rephrased SQL problem...
FromSent OnAttachments
Koo06 Aug 2000 17:56 
Ken Yiem06 Aug 2000 18:48 
Koo06 Aug 2000 19:16 
Ken Yiem06 Aug 2000 19:47 
Koo06 Aug 2000 20:11 
Bob Hall06 Aug 2000 20:33 
Ken Yiem06 Aug 2000 20:45 
Koo06 Aug 2000 22:03 
Jeremy Cole06 Aug 2000 22:39 
Subject:Re: rephrased SQL problem...
From:Koo (mys@sun-scope.com)
Date:08/06/2000 08:11:21 PM
List:com.mysql.lists.mysql

Thanks Ken. :-)

Closer...! But still not getting what I wanted. If I can select the following table again by something like:

select type,count(*) from {the following resulted table} group by type;

then... that reaches my goal!! BUT HOW DO I DO THAT? select a select? select upon a temp table..? What's the syntax?

Help please...!

Regards, Koo

(Note that I added "items.itemID" in "SELECT" statement to show explicitly what items are actually being selected) mysql> select type,items.itemID,count(items.itemID) -> from words,types,items,words_items -> where words.word like 'a%' -> and words_items.itemID=items.itemID -> and types.typeID=items.typeID -> and words.wordID=words_items.wordID -> group by type,items.itemID; +----------------+--------+---------------------+ | type | itemID | count(items.itemID) | +----------------+--------+---------------------+ | Joke | 3 | 5 | | Novel | 1 | 7 | | Novel | 2 | 2 | | Short Messages | 4 | 3 | +----------------+--------+---------------------+ 4 rows in set (0.01 sec)

Ken Yiem wrote:

That's the same version I'm using.

Try this . SELECT type,count(itemID) FROM bla bla WHERE bla bla GROUP BY type, itemID;

might get you closer :>

Cheers Ken

------------------------------------------------------------------------- www.mysqlwebring.com

-------------------------------------------------------------------------

work.. not at least with my version of MySQL (3.22.32).

mysql> select type,count(distinct(items.itemID)) -> from words,types,items,words_items -> where words.word like 'a%' -> and words_items.itemID=items.itemID -> and types.typeID=items.typeID -> and words.wordID=words_items.wordID -> group by type; ERROR 1064: You have an error in your SQL syntax near 'distinct(items.itemID)) from words,types,items,words_items where words.word like' at line 1

I'm not good in complex queries (in fact I don't even know how complex the query has to be for me to accomplish the job)

Any help?

Thanks, Koo

Ken Yiem wrote:

Have you tyied : SELECT type,COUNT(DISTINCT(items.itmeID)) FROM words,types,items,words_items WHERE bla bla ... GROUP BY type ;

This hasn't been tested as my server doesn't support DISTINCT within COUNT . only version 3.23.1 and above (i think).

if you take out the DISTINCT ... the resultant table should be +----------------+--------+ | type | itemID | +----------------+--------+ | Joke | 6 | | Novel | 9 | | Short Messages | 3 |

it doesn't count the distince itemID .. i'm hope'n the distince will work for you .

let me know ..

Cheers Ken

------------------------------------------------------------------------------ www.mysqlwebring.com

------------------------------------------------------------------------------

Hi, I rephrased my previous question in the hope of getting some feedback from you SQL experts. :-)

A simple query against my database outputs the following: mysql> select type,items.itemID from words,types,items,words_items -> where words.word like 'a%' -> and words.wordID=words_items.wordID -> and types.typeID=items.typeID -> and words_items.itemID=items.itemID -> order by type; +----------------+--------+ | type | itemID | +----------------+--------+ | Joke | 3 | | Joke | 3 | | Joke | 3 | | Joke | 3 | | Joke | 3 | | Novel | 1 | | Novel | 1 | | Novel | 1 | | Novel | 1 | | Novel | 2 | | Novel | 1 | | Novel | 1 | | Novel | 2 | | Novel | 1 | | Short Messages | 4 | | Short Messages | 4 | | Short Messages | 4 | +----------------+--------+

PROBLEM: How do I query my database so that I can find out the number of DISTINCT "items" in each "type" containing the word beginning with "a"?

I've tried many different combination of query statements similar to the following but still can't get what I expected. (the "no" is always incorrect)

mysql>select type, count(???) as no from ... . .. . . where words.word like 'a%' and .... ... . . . group by type;

The desired query *should* output the following result: +----------------+----+ | type | no | +----------------+----+ | Joke | 1 |(Only ONE distinct item, i.e. itemID=3) | Novel | 2 |(TWO distinct items, i.e. itemID=1 and itemID=2) | Short Messages | 1 |(Only ONE distinct item, i.e. itemID=4) +----------------+----+

Any help is highly appreciated.

Thanks and regards, Koo

###################################################################### CREATE TABLE items ( itemID int(8) DEFAULT '0' NOT NULL, typeID char(2) DEFAULT '0' NOT NULL, message text, PRIMARY KEY (itemID) );

INSERT INTO items VALUES (1,'1','this is almost about test alike'); INSERT INTO items VALUES (2,'1','hello baby'); INSERT INTO items VALUES (3,'2','two people alike'); INSERT INTO items VALUES (4,'3','alike baby hahaha');

CREATE TABLE types ( typeID int(11) DEFAULT '0' NOT NULL, type varchar(35), PRIMARY KEY (typeID) );

INSERT INTO types VALUES (1,'Novel'); INSERT INTO types VALUES (2,'Joke'); INSERT INTO types VALUES (3,'Short Messages'); INSERT INTO types VALUES (4,'Guestbook');

CREATE TABLE words ( wordID int(10) unsigned DEFAULT '0' NOT NULL auto_increment, word varchar(100) DEFAULT '' NOT NULL, PRIMARY KEY (wordID), UNIQUE word (word) );

INSERT INTO words VALUES (1,'about'); INSERT INTO words VALUES (2,'alike'); INSERT INTO words VALUES (3,'baby'); INSERT INTO words VALUES (4,'almost'); INSERT INTO words VALUES (5,'asad'); INSERT INTO words VALUES (6,'asadf'); INSERT INTO words VALUES (7,'asdffsa'); INSERT INTO words VALUES (8,'a908as'); INSERT INTO words VALUES (9,'ajkl8a'); INSERT INTO words VALUES (10,'a7jlasf');

CREATE TABLE words_items ( wordID int(10) unsigned DEFAULT '0' NOT NULL, itemID int(10) unsigned DEFAULT '0' NOT NULL, PRIMARY KEY (wordID,itemID) );

INSERT INTO words_items VALUES (1,1); INSERT INTO words_items VALUES (2,1); INSERT INTO words_items VALUES (2,3); INSERT INTO words_items VALUES (2,4); INSERT INTO words_items VALUES (3,1); INSERT INTO words_items VALUES (3,2); INSERT INTO words_items VALUES (3,4); INSERT INTO words_items VALUES (4,1); INSERT INTO words_items VALUES (5,1); INSERT INTO words_items VALUES (5,2); INSERT INTO words_items VALUES (5,3); INSERT INTO words_items VALUES (6,1); INSERT INTO words_items VALUES (7,1); INSERT INTO words_items VALUES (7,2); INSERT INTO words_items VALUES (7,3); INSERT INTO words_items VALUES (7,4); INSERT INTO words_items VALUES (8,3); INSERT INTO words_items VALUES (8,4); INSERT INTO words_items VALUES (9,3); INSERT INTO words_items VALUES (10,1);

--

--------------------------------------------------------------------- Please check "http://www.mysql.com/php/manual.php" before posting. To request this thread, e-mail mysq@lists.mysql.com

To unsubscribe, send a message to: <mysql-unsubscribe-##L=##H@lists.mysql.com>

If you have a broken mail client that cannot send a message to the above
address(Microsoft Outlook), you can use
http://lists.mysql.com/php/unsubscribe.php

--

--------------------------------------------------------------------- Please check "http://www.mysql.com/php/manual.php" before posting. To request this thread, e-mail mysq@lists.mysql.com

To unsubscribe, send a message to the address shown in the List-Unsubscribe header of this message. If you cannot see it, e-mail mysq@lists.mysql.com instead.

If you have a broken e-mail client that cannot send a message to the above
address (Microsoft Outlook), go to http://lists.mysql.com/php/unsubscribe.php