Hi,
Using sub-selects (MySQL 4.1 and higher) you can use something like

select count(*)/(select count(*) from table where field1='myvalue') as 
percentage from table where category='myvalue' group by category;

but I don't think you will gain much in performance this way. I'd rather use 
two queries - one for the total and one for the percentages. If field1 is 
indexed 

select count(*) from table where field1='myvalue' 

should be quite fast, so I don't think you should worry about having an 
additional query.

HTH
-- 
Dobromir Velev
[EMAIL PROTECTED]
http://www.websitepulse.com/

> and from a script I calculated my percentage = bigToal/categoryTotal

On Friday 08 October 2004 10:48, Jacques Jocelyn wrote:
> Hello there,
>
> Until  I  had  query  time  restriction  due to my web hoster. Meaning
> whenever the query was too long, it gets killed !
> Now I have changed my webhoster, I'd like to optimize my queries and I
> would like to have your opinion on that.
> I  wrote multiple queries to prevent any long query time duration such
> as :
> until now I did the following
> - to obtain the total of item which match requirements
> -> select count(*) 'bigTotal' from table where field1='myvalue'
>
> then  I  selected  total  of   category from the same table to get the
> percentage of the total
> such as :
> -> select count(*) 'categoryTotal' from table where category1='myvalue'
> and from a script I calculated my percentage = bigToal/categoryTotal
>
> -> select count(*) 'categoryTotal' from table where category2='myvalue'
> and from a script I calculated my percentage = bigToal/categoryTotal
> etc..
>
> now, I have planned the following : create ONE query to do all this.
> is there a way then to merge the two previous queries in only one ?
>
> and calculate the percentage at the same time ?
> To  merge all my category queries, I can use a GROUP BY, but what
> about the bigTotal, can have that in the same query ?
>
> Please advise.
> Thanks
>
>
> Best regards,
> Jacques Jocelyn


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

Reply via email to