SQL query problem

2007-11-14 Thread Matthew Stuart
Hi, I have built a site with Dreamweaver and I have a problem with a  
query.


I am trying to pass a parameter from one page to another to drill  
down. Basically, I have one product entry that is in multiple  
categories on my website. So, say it's a dress, it is therefore  
related to category 1 which is 'Girls', but it is also more  
specifically related to category 2 which is 'Girls Dresses'.


The way I have set this up is to have a column called MultiCategoryID  
that holds both the number 1 and 2 like this: /1/2/


When a user clicks a link to look at dresses, the parameter 2 is  
passed, but my query on the result page is wrong in some way because  
no records are displaying even though there is content to display.  
This is what I have so far:


SELECT *
FROM Products
WHERE MultiCategoryID LIKE '/catdrill/'
ORDER BY ProductID DESC

The parameter settings are:
Name: catdrill
Type: Numeric
Value: Request(MCID) MCID is the url parameter being passed
Default value: 2

Only when I test the Default value with an exact match of /1/2/ does  
any product display. What have I done wrong here? Is there a way to  
get it to recognise that I want it to pick specific numbers between  
the slashes rather than the whole lot? I have tried to change the  
slashes to full stops just in case they are causing problems, but  
it's still giving the same problem.


Thanks.

Mat



Re: SQL query problem

2007-11-14 Thread Ravi Kumar.
Dear Mat,

Your mail is not very clear. But I have a feeling that using '%' wildcard in
the like operand should help you

Regards,

Ravi.

On 11/14/07, Matthew Stuart [EMAIL PROTECTED] wrote:

 Hi, I have built a site with Dreamweaver and I have a problem with a
 query.

 I am trying to pass a parameter from one page to another to drill
 down. Basically, I have one product entry that is in multiple
 categories on my website. So, say it's a dress, it is therefore
 related to category 1 which is 'Girls', but it is also more
 specifically related to category 2 which is 'Girls Dresses'.

 The way I have set this up is to have a column called MultiCategoryID
 that holds both the number 1 and 2 like this: /1/2/

 When a user clicks a link to look at dresses, the parameter 2 is
 passed, but my query on the result page is wrong in some way because
 no records are displaying even though there is content to display.
 This is what I have so far:

 SELECT *
 FROM Products
 WHERE MultiCategoryID LIKE '/catdrill/'
 ORDER BY ProductID DESC

 The parameter settings are:
 Name: catdrill
 Type: Numeric
 Value: Request(MCID) MCID is the url parameter being passed
 Default value: 2

 Only when I test the Default value with an exact match of /1/2/ does
 any product display. What have I done wrong here? Is there a way to
 get it to recognise that I want it to pick specific numbers between
 the slashes rather than the whole lot? I have tried to change the
 slashes to full stops just in case they are causing problems, but
 it's still giving the same problem.

 Thanks.

 Mat




SQL-Query problem

2005-02-22 Thread Joppe A
Hello all,

Have a little problem with to make a sql-query as I want to have it... 
The problem is I need to check in 3 tables and count out and get it presentated 
per n_id like 

n_id  counted
01  5
02 10
03  2

My tables look as follows...

In sub:

id
n_id


In us:

id
email

In sub_del:

n_id
id

I have already a query that count it out totally.

SELECT (SELECT count(sub_id) from sub) - (SELECT count(us.id) FROM us LEFT JOIN 
sub ON sub.id = us.id)+(SELECT count(*) FROM sub_del);

But my problem is that I want to be able to get out the result per sub.n_id 
instaead.

Please help!

/Joppe
-- 
___
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



SQL Query problem

2004-02-20 Thread Claire Lee
Hi All,
I have a query problem here. Say I have a table with
employee records of three different departments. If
each department manager wants to see employee info of
their own department. Three different queries will be
needed. Is there a way that I can write one single
query and let SQL decide which department info to
display at the run time? Thanks.

__
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: SQL Query problem

2004-02-20 Thread Duncan Hill
On Friday 20 February 2004 15:19, Claire Lee wrote:
 Hi All,
 I have a query problem here. Say I have a table with
 employee records of three different departments. If
 each department manager wants to see employee info of
 their own department. Three different queries will be
 needed. Is there a way that I can write one single
 query and let SQL decide which department info to
 display at the run time? Thanks.

What language are you doing this in?  You should be able to provide the 
appropriate 'where' clause when generating the query.

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



sql Query problem?

2003-06-12 Thread Nishant

Hi,
I have a sql query problem... user points table, which I am
sorting(order by) points. Now if I want to have some 5 records above and
below a certain member, how could I write the sql query for the same?
say I want 5 records above and below NICKNAME_14!

thanks  and regards,

point   nickname
--
999 NICKNAME_6
980 NICKNAME_23
970 NICKNAME_8
960 NICKNAME_9
940 NICKNAME_11
940 NICKNAME_41
932 NICKNAME_26
930 NICKNAME_12
930 NICKNAME_42
922 NICKNAME_38
920 NICKNAME_13
900 NICKNAME_2
900 NICKNAME_25
900 NICKNAME_5
===
900 NICKNAME_14

900 NICKNAME_3
880 NICKNAME_30
860 NICKNAME_57
860 NICKNAME_39
860 NICKNAME_21
859 NICKNAME_36
850 NICKNAME_20
840 NICKNAME_35
840 NICKNAME_19
836 NICKNAME_45
830 NICKNAME_33
830 NICKNAME_18
820 NICKNAME_17
810 NICKNAME_32
800 NICKNAME_24
800 NICKNAME_15
764 NICKNAME_54
700 NICKNAME_27
625 NICKNAME_59
600 NICKNAME_28
563 NICKNAME_55
500 NICKNAME_29
270 NICKNAME_49
270 NICKNAME_48
262 NICKNAME_56
260 NICKNAME_44
260 NICKNAME_51
260 NICKNAME_47
256 NICKNAME_50
250 NICKNAME_58
246 NICKNAME_43
226 NICKNAME_60
140 NICKNAME_53


Nishant [EMAIL PROTECTED]





-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



RE: Complex SQL query problem...

2002-09-23 Thread Richard Bolen

FYI - this query seemed to work.

select * from nodes 
left join nodes as n2 on n2.parent_id = nodes.node_id 
left join jobs on jobs.parent_id = nodes.node_id 
left join colors on colors.parent_id = nodes.node_id 
where nodes.node_id = ? 
and ((n2.parent_id is not NULL) or (jobs.parent_id is not NULL) or (colors.parent_id 
is not NULL))


I need to do some more testing to be sure.

Rich

-Original Message-
From: Edward Peloke [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 23, 2002 08:46
To: Richard Bolen
Subject: RE: Complex SQL query problem...


After I sent this it hit me that it may not work if the first table (jobs)
contained no rows...I believe this would only work if the tables left joined
were empty not the jobs table.  sorry...

I apologize  did not respond Friday but I left work at 4.

Eddie

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 4:19 PM
To: Edward Peloke
Subject: RE: Complex SQL query problem...


Does this handle the case where the ID is in the submissions table but not
the jobs table?  How would this look if there was a third table also?

Thanks again for you help!

Rich

-Original Message-
From: Edward Peloke [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 15:59
To: MySQL Mailing List (E-mail)
Subject: RE: Complex SQL query problem...


try a left join

select count(*) from jobs
 left join submissions on
   jobs.standard_id=submissions.color_id
   where jobs.standard_id=ID_VALUE and submissions.color_id is null


Eddie

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 2:37 PM
To: MySQL Mailing List (E-mail)
Subject: Complex SQL query problem...


I'm trying to use a sql query to determine if an ID exists in any of 3
different tables in the database.  I need to do this in one SQL query
(ideally only using the ID once in the query).  I'm using mysql 3.23.47.

Here's an example of a query I came up with:

select count(*) from jobs, submissions where ID_VALUE in (jobs.standard_id,
submissions.color_id)


I'm just trying to determine if the ID exists.

This query works *IF AND ONLY IF* there is at least one record in each of
the tables.  If any of the table are empty, this query always returns a
count of 0 (even if there is a match in one of the non-empty tables).

Does anyone know why this is happening or could someone suggest a alternate
query?

Thanks,
Rich


Rich Bolen
Senior Software Developer
GretagMacbeth Advanced Technologies Center
79 T. W. Alexander Drive - Bldg. 4401 - Suite 250
PO Box 14026
Research Triangle Park, North Carolina 27709-4026  USA
Phone:  919-549-7575 x239,  Fax: 919-549-0421

http://www.gretagmacbeth.com/



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: Complex SQL query problem...

2002-09-23 Thread Edward Peloke

great!  hope it works !

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 23, 2002 9:17 AM
To: MySQL Mailing List (E-mail)
Cc: Edward Peloke
Subject: RE: Complex SQL query problem...


FYI - this query seemed to work.

select * from nodes
left join nodes as n2 on n2.parent_id = nodes.node_id
left join jobs on jobs.parent_id = nodes.node_id
left join colors on colors.parent_id = nodes.node_id
where nodes.node_id = ?
and ((n2.parent_id is not NULL) or (jobs.parent_id is not NULL) or
(colors.parent_id is not NULL))


I need to do some more testing to be sure.

Rich

-Original Message-
From: Edward Peloke [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 23, 2002 08:46
To: Richard Bolen
Subject: RE: Complex SQL query problem...


After I sent this it hit me that it may not work if the first table (jobs)
contained no rows...I believe this would only work if the tables left joined
were empty not the jobs table.  sorry...

I apologize  did not respond Friday but I left work at 4.

Eddie

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 4:19 PM
To: Edward Peloke
Subject: RE: Complex SQL query problem...


Does this handle the case where the ID is in the submissions table but not
the jobs table?  How would this look if there was a third table also?

Thanks again for you help!

Rich

-Original Message-
From: Edward Peloke [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 15:59
To: MySQL Mailing List (E-mail)
Subject: RE: Complex SQL query problem...


try a left join

select count(*) from jobs
 left join submissions on
   jobs.standard_id=submissions.color_id
   where jobs.standard_id=ID_VALUE and submissions.color_id is null


Eddie

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 2:37 PM
To: MySQL Mailing List (E-mail)
Subject: Complex SQL query problem...


I'm trying to use a sql query to determine if an ID exists in any of 3
different tables in the database.  I need to do this in one SQL query
(ideally only using the ID once in the query).  I'm using mysql 3.23.47.

Here's an example of a query I came up with:

select count(*) from jobs, submissions where ID_VALUE in (jobs.standard_id,
submissions.color_id)


I'm just trying to determine if the ID exists.

This query works *IF AND ONLY IF* there is at least one record in each of
the tables.  If any of the table are empty, this query always returns a
count of 0 (even if there is a match in one of the non-empty tables).

Does anyone know why this is happening or could someone suggest a alternate
query?

Thanks,
Rich


Rich Bolen
Senior Software Developer
GretagMacbeth Advanced Technologies Center
79 T. W. Alexander Drive - Bldg. 4401 - Suite 250
PO Box 14026
Research Triangle Park, North Carolina 27709-4026  USA
Phone:  919-549-7575 x239,  Fax: 919-549-0421

http://www.gretagmacbeth.com/



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Complex SQL query problem...

2002-09-20 Thread Richard Bolen

I'm trying to use a sql query to determine if an ID exists in any of 3 different 
tables in the database.  I need to do this in one SQL query (ideally only using the ID 
once in the query).  I'm using mysql 3.23.47.

Here's an example of a query I came up with:

select count(*) from jobs, submissions where ID_VALUE in (jobs.standard_id, 
submissions.color_id)


I'm just trying to determine if the ID exists.  

This query works *IF AND ONLY IF* there is at least one record in each of the tables.  
If any of the table are empty, this query always returns a count of 0 (even if there 
is a match in one of the non-empty tables).

Does anyone know why this is happening or could someone suggest a alternate query?

Thanks,
Rich 


Rich Bolen
Senior Software Developer
GretagMacbeth Advanced Technologies Center
79 T. W. Alexander Drive - Bldg. 4401 - Suite 250
PO Box 14026
Research Triangle Park, North Carolina 27709-4026  USA
Phone:  919-549-7575 x239,  Fax: 919-549-0421   

http://www.gretagmacbeth.com/   



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: Complex SQL query problem...

2002-09-20 Thread Edward Peloke

try a left join

select count(*) from jobs
 left join submissions on
   jobs.standard_id=submissions.color_id
   where jobs.standard_id=ID_VALUE and submissions.color_id is null


Eddie

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 2:37 PM
To: MySQL Mailing List (E-mail)
Subject: Complex SQL query problem...


I'm trying to use a sql query to determine if an ID exists in any of 3
different tables in the database.  I need to do this in one SQL query
(ideally only using the ID once in the query).  I'm using mysql 3.23.47.

Here's an example of a query I came up with:

select count(*) from jobs, submissions where ID_VALUE in (jobs.standard_id,
submissions.color_id)


I'm just trying to determine if the ID exists.

This query works *IF AND ONLY IF* there is at least one record in each of
the tables.  If any of the table are empty, this query always returns a
count of 0 (even if there is a match in one of the non-empty tables).

Does anyone know why this is happening or could someone suggest a alternate
query?

Thanks,
Rich


Rich Bolen
Senior Software Developer
GretagMacbeth Advanced Technologies Center
79 T. W. Alexander Drive - Bldg. 4401 - Suite 250
PO Box 14026
Research Triangle Park, North Carolina 27709-4026  USA
Phone:  919-549-7575 x239,  Fax: 919-549-0421

http://www.gretagmacbeth.com/



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: Complex SQL query problem...

2002-09-20 Thread Richard Bolen

Sorry about emailing you directly Eddie.  I meant to reply to the list with my last 
email.  

Anyway - your suggestion worked wonderfully.  Many many thanks.

-Original Message-
From: Edward Peloke [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 15:59
To: MySQL Mailing List (E-mail)
Subject: RE: Complex SQL query problem...


try a left join

select count(*) from jobs
 left join submissions on
   jobs.standard_id=submissions.color_id
   where jobs.standard_id=ID_VALUE and submissions.color_id is null


Eddie

-Original Message-
From: Richard Bolen [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 2:37 PM
To: MySQL Mailing List (E-mail)
Subject: Complex SQL query problem...


I'm trying to use a sql query to determine if an ID exists in any of 3
different tables in the database.  I need to do this in one SQL query
(ideally only using the ID once in the query).  I'm using mysql 3.23.47.

Here's an example of a query I came up with:

select count(*) from jobs, submissions where ID_VALUE in (jobs.standard_id,
submissions.color_id)


I'm just trying to determine if the ID exists.

This query works *IF AND ONLY IF* there is at least one record in each of
the tables.  If any of the table are empty, this query always returns a
count of 0 (even if there is a match in one of the non-empty tables).

Does anyone know why this is happening or could someone suggest a alternate
query?

Thanks,
Rich


Rich Bolen
Senior Software Developer
GretagMacbeth Advanced Technologies Center
79 T. W. Alexander Drive - Bldg. 4401 - Suite 250
PO Box 14026
Research Triangle Park, North Carolina 27709-4026  USA
Phone:  919-549-7575 x239,  Fax: 919-549-0421

http://www.gretagmacbeth.com/



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail
[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




A small SQL query problem

2002-04-12 Thread Torkil Johnsen

A small SQL query problem concerning articles and article_comments :) You
experts will have no problem with this :)


In brief:
-
I have no problems displaying the title, date, author, summary of the
articles or anything. Its just doing an sql query that gets my 3 latest
articles AND counts how many comments each article has, and doing it in ONE
query instead of two separate ones... :(


In *not* brief:
---
I have 2 tables.
One contains articles, basically like this:
---
| id  |  title  |  summary | text  |  date  | author  |
---

The other containts comments to the articles, basically looks like this:

---
| id | article_id | text | author |
---

Now, on my main page I want to display the first 3 articles like this:

TITLETITLETITLETITLETITLE (date)
written by AUTHOR

SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
 Read the whole story (comments: ##)

My actual problem is how to get the number of comments (##)!

I have no problems displaying the title, date, author, summary or anything.
Its just doing an sql query that counts how many comments the article has,
and doing it in ONE query instead of two separate ones... :(


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail mysql-unsubscribe-##L=##[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: A small SQL query problem

2002-04-12 Thread Roger Baklund

* Torkil Johnsen
[...]
 I have no problems displaying the title, date, author, summary of the
 articles or anything. Its just doing an sql query that gets my 3 latest
 articles AND counts how many comments each article has, and doing 
 it in ONE query instead of two separate ones... :(
[...]
 One contains articles, basically like this:
 ---
 | id  |  title  |  summary | text  |  date  | author  |
 ---
 
 The other containts comments to the articles, basically looks like this:
 
 ---
 | id | article_id | text | author |
 ---
 
 Now, on my main page I want to display the first 3 articles like this:
 
 TITLETITLETITLETITLETITLE (date)
 written by AUTHOR
 
 SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
 SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
 SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
  Read the whole story (comments: ##)
 
 My actual problem is how to get the number of comments (##)!

Try something like this:

SELECT a.title,a.date,a.author,a.summary,COUNT(c.id) as comments
  FROM articles as a,article_comments as c
  WHERE a.id = c.article_id
  GROUP BY a.title,a.date,a.author,a.summary
  ORDER BY a.date DESC
  LIMIT 3

--
Roger

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail mysql-unsubscribe-##L=##[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: A small SQL query problem

2002-04-12 Thread Torkil Johnsen

Thanks for the suggestion! I modified your query to something that works for
me (I was inaccurate with the column names before, sorry... :)

SELECT a.article_id,a.title,a.date,a.summary,COUNT(c.id) as comments
FROM articles as a,article_comments as c
WHERE a.article_id = c.article_id
GROUP BY a.article_id,a.title,a.date,a.summary
ORDER BY a.date DESC
LIMIT 3

This works PERFECTLY
Except:
If an article has NO comments, it is not returned at all!

So: When my articles table contains only 3 articles, and I make a query that
has LIMIT 3, you would think that this query would return all articles, but
it does not: Only the ones that actually has one or more comments.

I tried this with 3 articles where 2 of them had comments: Only 2 rows
returned. When I gave one comment on the last article: 3 rows returned.

Seems like if COUNT(c.id) returns 0, then the row is not returned at all.

Suggestions?

* Torkil Johnsen
[...]
 I have no problems displaying the title, date, author, summary of the
 articles or anything. Its just doing an sql query that gets my 3 latest
 articles AND counts how many comments each article has, and doing
 it in ONE query instead of two separate ones... :(
[...]
 One contains articles, basically like this:
 ---
 | id  |  title  |  summary | text  |  date  | author  |
 ---

 The other containts comments to the articles, basically looks like this:

 ---
 | id | article_id | text | author |
 ---

 Now, on my main page I want to display the first 3 articles like this:

 TITLETITLETITLETITLETITLE (date)
 written by AUTHOR

 SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
 SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
 SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY SUMMARY
  Read the whole story (comments: ##)

 My actual problem is how to get the number of comments (##)!

**ROGER
Try something like this:

SELECT a.title,a.date,a.author,a.summary,COUNT(c.id) as comments
  FROM articles as a,article_comments as c
  WHERE a.id = c.article_id
  GROUP BY a.title,a.date,a.author,a.summary
  ORDER BY a.date DESC
  LIMIT 3

--
Roger


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail mysql-unsubscribe-##L=##[EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: A small SQL query problem

2002-04-12 Thread John Klein

[EMAIL PROTECTED] wrote:
 
 Thanks for the suggestion! I modified your query to something that works for
 me (I was inaccurate with the column names before, sorry... :)
 
 SELECT a.article_id,a.title,a.date,a.summary,COUNT(c.id) as comments
 FROM articles as a,article_comments as c
 WHERE a.article_id = c.article_id
 GROUP BY a.article_id,a.title,a.date,a.summary
 ORDER BY a.date DESC
 LIMIT 3
 
 This works PERFECTLY
 Except:
 If an article has NO comments, it is not returned at all!
 
 So: When my articles table contains only 3 articles, and I make a query that
 has LIMIT 3, you would think that this query would return all articles, but
 it does not: Only the ones that actually has one or more comments.
 
 I tried this with 3 articles where 2 of them had comments: Only 2 rows
 returned. When I gave one comment on the last article: 3 rows returned.
 
 Seems like if COUNT(c.id) returns 0, then the row is not returned at all.

That's because there's no row in the comment table to join on. You want to
use a LEFT JOIN here. So the revised query might look like:

SELECT a.article_id,a.title,a.date,a.summary,COUNT(c.id) AS comments
FROM articles AS a
LEFT JOIN article_comments AS c
  ON a.article_id = c.article_id
GROUP BY a.article_id,a.title,a.date,a.summary
ORDER BY a.date DESC
LIMIT 3

-- 
John Klein, Database Applications Developer |  Omnia Mutantur,
Systems Group - Harvard Law School  |  Nihil Interit

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: A small SQL query problem

2002-04-12 Thread Roger Baklund

* Torkil Johnsen
 This works PERFECTLY
 Except:
 If an article has NO comments, it is not returned!

Use a LEFT JOIN:

SELECT a.title,a.date,a.author,a.summary,COUNT(c.id) as comments
   FROM articles as a
   LEFT JOIN article_comments as c ON
 a.id = c.article_id
   GROUP BY a.title,a.date,a.author,a.summary
   ORDER BY a.date DESC
   LIMIT 3

This also eliminates the WHERE clause in this case, as the criteria is used
in the ON clause of the LEFT JOIN.

URL: http://www.mysql.com/doc/J/O/JOIN.html 

--
Roger



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




sql query problem

2001-08-16 Thread Fabian Groene

Dear group members,



I have a serious problem with an SQL query that has kept me busy for over a week. 
Doing my compusory service at a non-profit environmental organisation I was asked to 
experiment with SQL queries within the ArcView GIS program. My problem is quite simple 
indeed and only refers to the SQL query:


There is a table with quite a lot of columns. But only two of them are really 
important for my query: One field is a date field and the other one is a group name. 
My aim is to create a query that only selects the item with the most recent date. That 
is simple and can be handled by max(date). But only the most recent item from each 
group shall be given as a result of my query. That's also fine. With the query

select groupname,max(date) from databasename Group By groupname

I got what I wanted and was happy. BUT: As soon as I told the database to read out 
more fields it no longer worked to get the most recent item from the particular 
groups. Instead there were many items having the same group which I wanted to supress 
with max(date)

My query was in the form of select groupname,field1,fieldn,max(date) from 
databasename Group By groupname, field1,fieldn 

Has anyone an idea how to overcome this problem?


Thanks a lot in advance,



Fabian Groene


___
1.000.000 DM gewinnen - kostenlos tippen - http://millionenklick.web.de
[EMAIL PROTECTED], 8MB Speicher, Verschluesselung - http://freemail.web.de



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: sql query problem

2001-08-16 Thread Ian Barwick

On Thursday 16 August 2001 10:52, Fabian Groene wrote:


(snip)
 My problem is quite simple indeed and only refers to the SQL query:


 There is a table with quite a lot of columns. But only two of them are
 really important for my query: One field is a date field and the other one
 is a group name. My aim is to create a query that only selects the item
 with the most recent date. That is simple and can be handled by max(date).
 But only the most recent item from each group shall be given as a result of
 my query. That's also fine. 

If I understand you correctly you have a table somewhat like this:

+-++---+
| item| gdate  | gname |
+-++---+
| this| 2001-10-01 | ABC   |
| that| 2001-01-01 | ABC   |
| the | 2000-09-01 | Hello |
| other   | 2000-06-01 | Hello |
| no idea | 2000-10-10 | Hello |
+-++---+

(but probably more complex). For each group with in the column 'gname' you 
want to extract the item (or what other data you have) with the most recent 
date. So your desired result would be: 

+-++---+
| item| gdate  | gname |
+-++---+
| this| 2001-10-01 | ABC   |
| no idea | 2000-10-10 | Hello |
+-++---+

(Or am I missing something?)

 With the query

 select groupname,max(date) from databasename Group By groupname

 I got what I wanted and was happy. 

For the above example the query and result set looks like this:

mysql select gname, max(gdate) from groups group by gname; 
+---++
| gname | max(gdate) |
+---++
| ABC   | 2001-10-01 |
| Hello | 2000-10-10 |
+---++

Which, I agree, _looks_ right.

 BUT: As soon as I told the database to
 read out more fields it no longer worked to get the most recent item from
 the particular groups. Instead there were many items having the same group
 which I wanted to supress with max(date)

 My query was in the form of select groupname,field1,fieldn,max(date) from
 databasename Group By groupname, field1,fieldn 

So you ended up with something like this?

mysql select gname, max(gdate), item, gdate from groups group by gname, 
item, gdate;
+---++-++
| gname | max(gdate) | item| gdate  |
+---++-++
| ABC   | 2001-01-01 | that| 2001-01-01 |
| ABC   | 2001-10-01 | this| 2001-10-01 |
| Hello | 2000-10-10 | no idea | 2000-10-10 |
| Hello | 2000-06-01 | other   | 2000-06-01 |
| Hello | 2000-09-01 | the | 2000-09-01 |
+---++-++

 Has anyone an idea how to overcome this problem?

Yes and no. An obvious (but misleading solution) would be this:

mysql select gname, max(gdate), item, gdate from groups group by gname;
+---++--++
| gname | max(gdate) | item | gdate  |
+---++--++
| ABC   | 2001-10-01 | this | 2001-10-01 |
| Hello | 2000-10-10 | the  | 2000-09-01 |
+---++--++

(not sure whether that's valid SQL; applications like Oracle or PostgreSQL 
certainly would't accept the statement as it is).

Looks almost right, but for the group Hello we have completely the wrong 
item and date :-(

Unfortunately I don't think what you want to do can be done in MySQL. It's 
something I've run into once or twice and haven't been able to resolve. Thus 
far I've coded round it on the application side. Ideas anyone?

The usual solution would be a subselect a la:

  select gname, item, gdate 
from groups 
   where gdate in (select max(gdate) 
 from groups 
 group by gname)

Alas no subselects yet in MySQL (see:
 http://www.mysql.com/doc/M/i/Missing_Sub-selects.html )


HTH in some way anyway

Ian Barwick


-- 
Ian Barwick - Developer - [EMAIL PROTECTED]
akademie.de asp GmbH - http://www.akademie.de

To query tables in a MySQL database is more fun than eating spam

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: SQL query problem

2001-06-25 Thread Adrian D'Costa

On Fri, 22 Jun 2001, Thomas J Keller wrote:

 select zip, city, county from zipcodes where zip between 68400 and 68500;
 
 here is a portion of the garbled output:
 
  |68442 | STELLA   | RICHARDSON
 |43 | STERLING | JOHNSON
|444 | STRANG   | FILLMORE
  |5 | SWANTON  | SALINE
 +---+--+-+
Not too sure what could be wrong, but I did something like this:

mysql select * from country where contid between 10 and 20;
++--++
| id | country  | contid |
++--++
|  9 | Benin| 10 |
| 10 | Bermuda  | 11 |
| 11 | Birmania | 12 |
| 12 | Bolivia  | 13 |
| 13 | Botswana | 14 |
| 14 | Bulgaria | 15 |
| 15 | Burkina faso | 16 |
| 16 | Burundi  | 17 |
| 17 | Buthan   | 18 |
| 18 | Cambogia | 19 |
| 19 | Camerun  | 20 |
++--++
11 rows in set (0.00 sec)

It works!

My table is:
mysql desc country;
+-+-+--+-+-++-+
| Field   | Type| Null | Key | Default | Extra  |
Privileges
  |
+-+-+--+-+-++-+
| id  | int(5)  |  | PRI | NULL| auto_increment |
select,insert,update,references |
| country | varchar(35) |  | MUL | ||
select,insert,update,references |
| contid  | int(5)  |  | UNI | 0   ||
select,insert,update,references |
+-+-+--+-+-++-+

Similar to yours.  Does your table hold any data less that 65000?  

Try select zip, city, county from zipcodes where zip=68400 and zip=
68500;

Regards

Adrian


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




SQL query problem

2001-06-22 Thread Thomas J Keller

  I am using two versions of MySQL, on two different platforms, and having the
same problem on both.  One very odd aspect of this problem is that it behaves
precisely the same way on both platforms, down to which records are garbled
onscreen and how they are garbled.  Anyone with any ideas?   Thanks in advance

Platforms:

RedHat Linux 6.1   MySQL Ver. 3.23.28gamma  (locally compiled)
FreeBSD 3.4-STABLE MySQL Ver. 3.22.32

Problem:  when I use the following query, I get garbled data in the MySQL
command line client:

select zip, city, county from zipcodes where zip between 68400 and 68500;

here is a portion of the garbled output:

 |68442 | STELLA   | RICHARDSON
|43 | STERLING | JOHNSON
   |444 | STRANG   | FILLMORE
 |5 | SWANTON  | SALINE
   || SYRACUSE | OTOE
 |7 | TABLE ROCK   | PAWNEE
   || TALMAGE  | OTOE
|50 | TECUMSEH | JOHNSON
   || ONG  | CLAY
 |3 | TOBIAS   | SALINE
   || UNADILLA | OTOE
   || UNION| CASS
 |6 | UTICA| SEWARD
 |68457 | VERDON   | RICHARDSON
   || VIRGINIA | GAGE
   || WACO | YORK
  |8461 | WALTON   | LANCASTER
  |8462 | WAVERLY  | LANCASTER
   || WEEPING WATER| CASS
 |4 | WESTERN  | SALINE
 |5 | WILBER   | SALINE
   || WYMORE   | GAGE
   || YORK | YORK
+---+--+-+

BUT, if I use:  select zip, city, county from zipcodes where zip = 68465;  for
example  I get:

 |68465 | WILBER | SALINE 

  This holds for any specific zipcode in the database.  There is one range,
68500 through 58600 which
display correctly.  Oddly enough, that is the range for the city I am in.

   The zipcodes database looks like this:

mysql desc zipcodes;
+---++--+-+-+---+-+
| Field | Type   | Null | Key | Default | Extra |
Privileges  |
+---++--+-+-+---+-+
| zip   | int(11)|  | PRI | 0   |   |
select,insert,update,references |
| latitude  | float(7,4) | YES  | | NULL|   |
select,insert,update,references |
| longitude | float(8,4) | YES  | | NULL|   |
select,insert,update,references |
| city  | text   | YES  | | NULL|   |
select,insert,update,references |
| state | text   | YES  | | NULL|   |
select,insert,update,references |
| county| text   | YES  | | NULL|   |
select,insert,update,references |
+---++--+-+-+---+-+
6 rows in set (0.00 sec)

  This is a commercial database product named ZipPLUS.

   I also tried this:

mysql check table zipcodes;
++---+--+--+
| Table  | Op| Msg_type | Msg_text |
++---+--+--+
| build.zipcodes | check | status   | OK   |
++---+--+--+
1 row in set (0.91 sec)


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




SQL query problem with mysql.

2001-03-05 Thread Larry Kim

hi,

i'm writing a book (wrox publishers) which uses mysql for the sample
database.
i seem to have encountered a problem with an SQL query.
its a simple voting application, with a candidate table, and a vote table:

create table candidate (
candidatenumber integer not null auto_increment,
firstname varchar(32) not null,
lastname varchar(32) not null,
politicalparty varchar(32) not null,
primary key(candidatenumber));


create table votes (
votenumber integer not null auto_increment,
candidatenumber integer,
countynumber integer,
primary key(votenumber),
foreign key(candidatenumber) references candidate,
foreign key(countynumber) references county);


i want to do a query that shows firstname, lastname, the number of votes for
that guy, and the total number of votes cast as illustrated:

George, Bush, 2, 10
Al, Gore, 2, 10
Pat, Buchannan, 1, 10
Ralph, Nader, 5, 10

for example ralph nader received  5 votes out of a total of 10 cast.
Al gore received 2 votes out of 10 ... you get the idea.

here is my query:

SELECT Candidate.FIRSTNAME, Candidate.LASTNAME, count(Votes.VoteNumber) ,
count(select * from Votes)
FROM Candidate LEFT OUTER JOIN Votes ON Candidate.CANDIDATENUMBER =
Votes.CANDIDATENUMBER
GROUP BY Candidate.FIRSTNAME, Candidate.LASTNAME

everything works except for the nasty count(select (*) from votes) which
seems to work on other db's.
if i take it away then it works fine.  any ideas on how i could obtain a
count of the number of votes
cast?


thank you for your consideration.

Larry Kim
[EMAIL PROTECTED]
[EMAIL PROTECTED]


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: SQL query problem with mysql.

2001-03-05 Thread Aaron Weiker

I would make the select statement look like this

SELECT Candidate.FIRSTNAME, Candidate.LASTNAME, count(Votes.VoteNumber) ,
count(select votenumber from Votes)
FROM Candidate LEFT OUTER JOIN Votes ON Candidate.CANDIDATENUMBER =
Votes.CANDIDATENUMBER
GROUP BY Candidate.FIRSTNAME, Candidate.LASTNAME


By doing this you are using less resources the SQL server. Usually whenever
you do any select statement the sql engine will go retrieve all of the rows
you specify, even in a count. This will cause the SQL enginge to get every
row in this table. I also remember hearing someone mention that with MySQL
it is generally not a good idea to do a query with an (*) in it because it
could produce speratic data. So by doing a count on a particular column this
would alleviate that problem.

Aaron Weiker
Programmer
CISP - Changing Internet Speed  Performance

Phone: 419.724.5351 [EMAIL PROTECTED]
Pager: 419.218.0013 http://www.cisp.cc
Cell:419.304.0323   web search:
http://www.allthesites.com 

-Original Message-
From: Larry Kim [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 05, 2001 4:55 AM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: SQL query problem with mysql.


hi,

i'm writing a book (wrox publishers) which uses mysql for the sample
database.
i seem to have encountered a problem with an SQL query.
its a simple voting application, with a candidate table, and a vote table:

create table candidate (
candidatenumber integer not null auto_increment,
firstname varchar(32) not null,
lastname varchar(32) not null,
politicalparty varchar(32) not null,
primary key(candidatenumber));


create table votes (
votenumber integer not null auto_increment,
candidatenumber integer,
countynumber integer,
primary key(votenumber),
foreign key(candidatenumber) references candidate,
foreign key(countynumber) references county);


i want to do a query that shows firstname, lastname, the number of votes for
that guy, and the total number of votes cast as illustrated:

George, Bush, 2, 10
Al, Gore, 2, 10
Pat, Buchannan, 1, 10
Ralph, Nader, 5, 10

for example ralph nader received  5 votes out of a total of 10 cast.
Al gore received 2 votes out of 10 ... you get the idea.

here is my query:

SELECT Candidate.FIRSTNAME, Candidate.LASTNAME, count(Votes.VoteNumber) ,
count(select * from Votes)
FROM Candidate LEFT OUTER JOIN Votes ON Candidate.CANDIDATENUMBER =
Votes.CANDIDATENUMBER
GROUP BY Candidate.FIRSTNAME, Candidate.LASTNAME

everything works except for the nasty count(select (*) from votes) which
seems to work on other db's.
if i take it away then it works fine.  any ideas on how i could obtain a
count of the number of votes
cast?


thank you for your consideration.

Larry Kim
[EMAIL PROTECTED]
[EMAIL PROTECTED]


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php



Re: SQL query problem with mysql.

2001-03-05 Thread Jason Landry

I think you really need another couple tables, even though your just writing
a sample.

You probably need a table named 'election' and one named
'electionparticipants'

Your election table would contain things like the date of the election, the
total number of votes cast, etc.
The electionparticipants table would be a simple join table between
candidate and election.

Without those two tables, you're going to have some issues trying to do what
you want.

- Original Message -
From: "Larry Kim" [EMAIL PROTECTED]
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Sent: Monday, March 05, 2001 3:54 AM
Subject: SQL query problem with mysql.


 hi,

 i'm writing a book (wrox publishers) which uses mysql for the sample
 database.
 i seem to have encountered a problem with an SQL query.
 its a simple voting application, with a candidate table, and a vote table:

 create table candidate (
 candidatenumber integer not null auto_increment,
 firstname varchar(32) not null,
 lastname varchar(32) not null,
 politicalparty varchar(32) not null,
 primary key(candidatenumber));


 create table votes (
 votenumber integer not null auto_increment,
 candidatenumber integer,
 countynumber integer,
 primary key(votenumber),
 foreign key(candidatenumber) references candidate,
 foreign key(countynumber) references county);


 i want to do a query that shows firstname, lastname, the number of votes
for
 that guy, and the total number of votes cast as illustrated:

 George, Bush, 2, 10
 Al, Gore, 2, 10
 Pat, Buchannan, 1, 10
 Ralph, Nader, 5, 10

 for example ralph nader received  5 votes out of a total of 10 cast.
 Al gore received 2 votes out of 10 ... you get the idea.

 here is my query:

 SELECT Candidate.FIRSTNAME, Candidate.LASTNAME, count(Votes.VoteNumber) ,
 count(select * from Votes)
 FROM Candidate LEFT OUTER JOIN Votes ON Candidate.CANDIDATENUMBER =
 Votes.CANDIDATENUMBER
 GROUP BY Candidate.FIRSTNAME, Candidate.LASTNAME

 everything works except for the nasty count(select (*) from votes) which
 seems to work on other db's.
 if i take it away then it works fine.  any ideas on how i could obtain a
 count of the number of votes
 cast?


 thank you for your consideration.

 Larry Kim
 [EMAIL PROTECTED]
 [EMAIL PROTECTED]


 -
 Before posting, please check:
http://www.mysql.com/manual.php   (the manual)
http://lists.mysql.com/   (the list archive)

 To request this thread, e-mail [EMAIL PROTECTED]
 To unsubscribe, e-mail
[EMAIL PROTECTED]
 Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: SQL query problem with mysql.

2001-03-05 Thread Bob Hall

hi,

i'm writing a book (wrox publishers) which uses mysql for the sample
database.
i seem to have encountered a problem with an SQL query.
its a simple voting application, with a candidate table, and a vote table:

create table candidate (
candidatenumber integer not null auto_increment,
firstname varchar(32) not null,
lastname varchar(32) not null,
politicalparty varchar(32) not null,
primary key(candidatenumber));


create table votes (
votenumber integer not null auto_increment,
candidatenumber integer,
countynumber integer,
primary key(votenumber),
foreign key(candidatenumber) references candidate,
foreign key(countynumber) references county);


i want to do a query that shows firstname, lastname, the number of votes for
that guy, and the total number of votes cast as illustrated:

George, Bush, 2, 10
Al, Gore, 2, 10
Pat, Buchannan, 1, 10
Ralph, Nader, 5, 10

for example ralph nader received  5 votes out of a total of 10 cast.
Al gore received 2 votes out of 10 ... you get the idea.

here is my query:

SELECT Candidate.FIRSTNAME, Candidate.LASTNAME, count(Votes.VoteNumber) ,
count(select * from Votes)
FROM Candidate LEFT OUTER JOIN Votes ON Candidate.CANDIDATENUMBER =
Votes.CANDIDATENUMBER
GROUP BY Candidate.FIRSTNAME, Candidate.LASTNAME

everything works except for the nasty count(select (*) from votes) which
seems to work on other db's.
if i take it away then it works fine.  any ideas on how i could obtain a
count of the number of votes
cast?


thank you for your consideration.

Larry Kim
[EMAIL PROTECTED]
[EMAIL PROTECTED]

Sir, MySQL doesn't (yet) support subqueries. Generally, you get 
around this by using TEMPORARY tables. The following, in slightly 
different form, ran successfully on the MS Titanic (aka my Wintel 
box).

CREATE TEMPORARY TABLE counts
SELECT Candidate.FIRSTNAME, Candidate.LASTNAME, count(*)
FROM Candidate LEFT JOIN Votes
   ON Candidate.CANDIDATENUMBER = Votes.CANDIDATENUMBER
GROUP BY Candidate.FIRSTNAME, Candidate.LASTNAME;

CREATE TEMPORARY TABLE total_votes
SELECT Count(*) AS total_votes FROM votes;

SELECT * FROM counts, total_votes;

You can also use

INSERT INTO counts
SELECT "Total", Count(*) FROM votes;

SELECT * FROM counts;

in place of the last two statements, which will give you the same 
data in a different format. Your readers may find the second solution 
easier to understand. The TEMPORARY tables are automatically removed 
when the connection ends.

Note that the FOREIGN KEY clause in your CREATE TABLE statement has 
no effect in MySQL. It is only there for compatibility with other 
RDBMSs. In order to maximize speed, MySQL provides no relational 
integrity constraints other than those applied to primary keys. 
Foreign keys are not enforced.

It is possible that before your book goes to the printer, MySQL will 
have added subqueries, and one developer or another will have created 
code that makes it possible to add table types that support integrity 
constraints. These things are all in the works from various sources.

Bob Hall

Know thyself? Absurd direction!
Bubbles bear no introspection. -Khushhal Khan Khatak

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




SQL query problem

2001-01-23 Thread Sander Pilon

Okay, here's one for the guru's out there :)

I have a list of entries with unique id numbers X, and a set of sort methods
(S1 ... Sy).

Now, if I want to get an entry at position P (0...z) in the list of
entries ordered by method S1 then I'd
make the following query:

SELECT X FROM table WHERE  ORDER BY S1 LIMIT P,1

But now I want the inverse - given an id X and a sort method, I want the
position.

something like: SELECT POSITION(X) FROM table WHERE . ORDER BY S1

Is there a way to do this?

-Sander


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: SQL query problem

2001-01-23 Thread Peter Pentchev

On Tue, Jan 23, 2001 at 05:39:47PM +0100, Sander Pilon wrote:
 Okay, here's one for the guru's out there :)
 
 I have a list of entries with unique id numbers X, and a set of sort methods
 (S1 ... Sy).
 
 Now, if I want to get an entry at position P (0...z) in the list of
 entries ordered by method S1 then I'd
 make the following query:
 
 SELECT X FROM table WHERE  ORDER BY S1 LIMIT P,1
 
 But now I want the inverse - given an id X and a sort method, I want the
 position.
 
 something like: SELECT POSITION(X) FROM table WHERE . ORDER BY S1
 
 Is there a way to do this?

You could try something like..

SELECT COUNT(X) FROM table
WHERE  AND X  (yourX)
ORDER BY S1

where X is the name of the field (literally), and yourX is the value
you're interested in.  After that, just add 1.

NOTE: This does not guarantee that yourX actually exists in the table;
if it doesn't, this will happily return the position yourX WOULD HAVE BEEN
at, had it existed.

To find out if it exists, you'll have to do a separate query.

G'luck,
Peter

-- 
The rest of this sentence is written in Thailand, on

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php