Jump to content

count(), left join..


want2php

Recommended Posts

Hi, newbie here.

 

I have 3 tables:

 

1. log

  - log_id

  - stat_id

  -times_tamp

2. status

  - stat_id

  - stat_label

3. detail

  - detail_id

  - stat_id

 

 

i would like to count the total number and display all data from log table that matches stat_id from status and detail table.  I was thinking of the left join statement but my newbie coding skills failed.

 

This is what ive done so far.

 

$sql = "SELECT log_id, stat_id, times_stamp FROM log, status, detail
          LEFT JOIN log on (log.stat_id = status.stat_id)
           where log.stat_id = status.stat_id
            and log.stat_id = detail.stat_id

 

and am stuck, don't know how to proceed from here.

 

any help is greatly appreciated. thanks.

Link to comment
Share on other sites

Not Sure what you want but would this help?

 

 $sql = "SELECT * FROM log LEFT JOIN status ON log.stat_id = status.stat_id LEFT JOIN detail ON status.stat_id = detail.stat_id WHERE log.stat_id = status.stat_id AND log.stat_id = detail.stat_id

 

Im not sure if this will help, but I do not really understand the problem if you could elborate more I may be able to help.

Link to comment
Share on other sites

Hi

 

I think you are looking for a count of the log records and a count of the detail records for each stat_id.

 

If so try this:-

 

SELECT a.stat_id, a.stat_label, b.detail_stat_count, c.log_stat_count
FROM status a
LEFT OUTER JOIN (SELECT stat_id, COUNT(*) AS detail_stat_count from detail GROUP BY stat_id) b ON a.stat_id = b.stat_id
LEFT OUTER JOIN ((SELECT stat_id, COUNT(*) AS log_stat_count from log GROUP BY stat_id) c ON a.stat_id = c.stat_id

 

All the best

 

Keith

Link to comment
Share on other sites

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.