Jump to content

Recommended Posts

Hi guys is there someway i can optimise/streamline this code...it seems a bit cumbersome, do i really have to write so many queries? There are quite a few more sectors..wondering if somehow i can get the results using only one sql statement:

 

example:

 

$result_contacts = mysql_query("SELECT * FROM company WHERE sector='Retail'");
echo "The number of companies in retail is: ".mysql_num_rows($result_contacts);

$result_contacts = mysql_query("SELECT * FROM company WHERE sector='Finance'");
echo "The number of companies in finance is: ".mysql_num_rows($result_contacts);

$result_contacts = mysql_query("SELECT * FROM company WHERE sector='Leisure'");
echo "The number of companies in Leisure is: ".mysql_num_rows($result_contacts);

 

Thanks in advance

 

Hi

 

Basic improvement:-

 

$result_contacts = mysql_query("SELECT count(*) AS RetailCount FROM company WHERE sector='Retail'");
if ($row = mysql_fetch_array($result_contacts))
echo "The number of companies in retail is: ".$row['RetailCount'];

$result_contacts = mysql_query("SELECT count(*) AS FinanceCount FROM company WHERE sector='Finance'");
if ($row = mysql_fetch_array($result_contacts))
echo "The number of companies in finance is: ".$row['FinanceCount'];

$result_contacts = mysql_query("SELECT count(*) AS LeisureCount FROM company WHERE sector='Leisure'");
if ($row = mysql_fetch_array($result_contacts))
echo "The number of companies in leisure is: ".$row['LeisureCount'];

 

but that is still 3 seperate calls. You could try:-

 

$result_contacts = mysql_query("SELECT (SELECT count(*)  FROM company WHERE sector='Retail') AS RetailCount, (SELECT count(*) FROM company WHERE sector='Finance') AS FinanceCount,(SELECT count(*) FROM company WHERE sector='Leisure') AS LeisureCount  ");
if ($row = mysql_fetch_array($result_contacts))
{
echo "The number of companies in retail is: ".$row['RetailCount'];
echo "The number of companies in finance is: ".$row['FinanceCount'];
echo "The number of companies in leisure is: ".$row['LeisureCount'];
}

 

All the best

 

Keith

thanks kickstart.. but Mchl's works perfectly and suits my needs as i have about 20 different sectors.... yours and mine would get pretty lengthy

 

It's ideal to use GROUP BY, as you can see it will scale if you add more sectors.

This would make it a bit better

$result_contacts = mysql_query("SELECT (SELECT sector,count(*) as Count FROM company  group by sector");
if ($row = mysql_fetch_array($result_contacts))
{
echo "The number of companies in ".$row['sector']." is: ".$row['Count'];

}

Hi

 

I would agree with the group by if you want more than a nominal number of counts (and with 20 and possibly growing you do), but if you are only looking at a fixed couple of groups and want to avoid a large result set and looping through them (and getting them in a specific odd order), then the single statement would do it well.

 

All the best

 

Keith

I would agree with the group by if you want more than a nominal number of counts (and with 20 and possibly growing you do), but if you are only looking at a fixed couple of groups and want to avoid a large result set and looping through them (and getting them in a specific odd order), then the single statement would do it well.

 

Look up the word "scalability". It's mentioned a few times in this topic already. Yours will also be slower. You're running four queries instead of one.

So basically SELECT sector, COUNT(*) AS companyCount FROM companies GROUP BY sector works great.. I have another quick question regarding joins

 

at the moment I get the result set looking something like:

 

Sector companyCount

Retail 10

Finance 20

Leisure 12

 

Which is great... now I have another table called contacts(which contains all the contacts i.e employees of these companies) and this ties to the companies table using the company column (which contains the company names) ...

 

I was hoping to get the number of employees in that particular Sector too so the above would look like:

 

Sector companyCount contactsCount

Retail 10 23

Finance 20 32

Leisure 12 11

 

 

How would i go about doing the Join statment..thanks for all your help appreciate it..

 

So for example in this query:

 

SELECT sector, COUNT(*) AS companyCount FROM companies JOIN contacts ON(companies.company = contact.company) GROUP BY sector

 

where/how would i put the count(*) for the contacts database in the above query to get the column contactsCount

 

 

 

 

Hi

 

I understand scaleability. The count(*) and group by would be doing multiple counts, which could (from his initial post) be completely wasted (ie, if there were multiple groups which were not required). Sure you could use an in statement to reduce the number to those required but not order them without an equally unscalable custom order by clause, or by joining to another table containing the order clause for the groups.

 

My suggestion was for a single query with some sub queries, resulting in a single row being returned and no need to loop round returning seperate rows. Should be no major difference in SQL load (possibly lower), but a saving in php processing. But as said pointless if you want more than a fixed nominal number of groups (but from his initial post it appeared that what he wanted was just a fixed tiny number of groups).

 

All the best

 

Keith

Turns out to be a MySQL question after all? ;)

 

That would look something along these lines

 

SELECT
  sector,
  COUNT(cm.company) AS companyCount,
  COUNT(ct.contact) AS contactCount 
FROM companies AS cm
LEFT JOIN
contacts AS ct
USING company

lol yeah looks like a mysql question more n more :)

 

i tried:

 

SELECT sector, COUNT(cm.company) AS companyCount, COUNT(ct.contact) AS contactCount FROM companies AS cm LEFT JOIN contacts AS ct USING company

 

but i get the error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'company' at line 1

 

The tables are called contacts and companies and the pk/fk between them is company column... am i missing something?

 

Thanks for you help..if the above works.. it will be awesome..uve saved me hours!!

 

what does ct.contact in COUNT(ct.contact) refer to?  I take it ct is the contacts table and contact is refering to a column in the contacts table? i dont have a contact column in the contact, i have id, first_name, surname, column etc etc..

 

Yep ur right kickstart cheers, just added that in.. 

 

thanks mate i think we are getting there:

 

I added the GROUP by sector back in at the end:

 

SELECT sector, COUNT(cm.company) AS companyCount, COUNT(ct.id) AS contactCount FROM companies AS cm LEFT JOIN contacts AS ct USING (company) GROUP by sector;

 

Im not getting an error but im getting both columns saying the same thing e.g

 

Sector                companyCount              contactsCount

Retail                          10                          10

Finance                        20                          20

Leisure                        12                          12

 

 

Obviously... that wouldn't work... Silly me ;P  How about

 

SELECT 
  cm.sector, 
  COUNT(cm.company) AS companyCount, 
  ct.contactCount 
FROM 
  companies AS cm 
LEFT JOIN 
(SELECT company, COUNT(*) AS contactCount FROM contacts GROUP BY company) AS ct
USING (company)
GROUP by sector;

Hi

 

Give this a try. This should give the same results (just to ensure that the problem is the SQL rather than just luck with the data).

 

SELECT sector, COUNT( * ) AS CompCount, (

SELECT COUNT( * ) 
FROM contacts z
JOIN companies y ON z.company = y.company
WHERE a.sector = y.sector
)
FROM companies a
GROUP BY sector

 

All the best

 

Keith

awesome thanks guys... kickstart yours seem to give the correct results, thanks so much for your help ... mcl thanks for all your help as well mate really appreciate it.. your results seem to give different results for some reason not sure why ...

 

i have to run to a meeting now..but i'll thoroughly test both solutions again and post back..

 

thanks again..

Probably needs some more tweaking and it will give correct results as well. It should also be faster than subquery in column list.

 

I can't see why your shouldn't work and it does appear more elegant.

 

Playing with it, it seems to be giving the count of the number of contacts for the first matching company. As though it is doing the grouping prior to the join.

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.