pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Selecting from two tables
3+ messages / 3 participants
[nested] [flat]

* Selecting from two tables
@ 2004-01-14 23:33 Michael Hanna <taojones@sympatico.ca>
  0 siblings, 0 replies; 3+ messages in thread

From: Michael Hanna @ 2004-01-14 23:33 UTC (permalink / raw)
  To: pgsql-php

  I have a table called Jobs and another called Applications. I can 
query all Job rows, but how do I also include the number of Application 
rows for each Job row in the SELECT statement?

applications has a jobid foreign key

create table job (      jobid SERIAL,
                         login varchar(40),
                         jobtitle varchar(70),
                         jobdescrip TEXT,
                         jobkeywords TEXT,
                         valid int2 NOT NULL,
                         postdate TIMESTAMPTZ,
                         PRIMARY KEY (jobid),
                         FOREIGN KEY(login) REFERENCES company
                             ON DELETE CASCADE
                         );


CREATE TABLE application        (   applid SERIAL,
                                     jobid INTEGER,
                                     login varchar(6),
                                     apldate TIMESTAMPTZ,
                                     PRIMARY KEY (applid),
                                     FOREIGN KEY (jobid) REFERENCES job
                                         ON DELETE CASCADE,
                                     FOREIGN KEY (login) REFERENCES 
student
                                         ON DELETE CASCADE
                                 );



thanks for any help on this..
Michael




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Selecting from two tables
@ 2004-01-15 07:44 mauro.folcarelli@sistinf.it
  0 siblings, 0 replies; 3+ messages in thread

From: mauro.folcarelli@sistinf.it @ 2004-01-15 07:44 UTC (permalink / raw)
  To: pgsql-php

>> I have a table called Jobs and another called Applications.
>> I can query all Job rows, but how do I also include the number
>> of Application rows for each Job row in the SELECT statement?

Try this way:

(1) CREATE VIEW viewappl AS
        SELECT jobid
              ,count(*) as numappl
        FROM application
        GROUP BY
               jobid;

(2) SELECT
  job.jobid
 ,job.login
 ,job.jobtitle
 ,job.jobdescrip
 ,job.jobkeywords
 ,job.valid
 ,job.postdate
 ,viewappl.numappl
 FROM job , viewappl
 where job.jobid = viewappl.jobid
 order by
  job.jobid
 ;

I hope it's ok.

Hi, Mauro




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Selecting from two tables
@ 2004-01-15 08:37 Nick Barr <nick.barr@webbased.co.uk>
  0 siblings, 0 replies; 3+ messages in thread

From: Nick Barr @ 2004-01-15 08:37 UTC (permalink / raw)
  To: 'Michael Hanna' <taojones@sympatico.ca>; pgsql-php

> -----Original Message-----
> From: pgsql-php-owner@postgresql.org [mailto:pgsql-php-
> owner@postgresql.org] On Behalf Of Michael Hanna
> Sent: 14 January 2004 23:33
> To: pgsql-php@postgresql.org
> Subject: [PHP] Selecting from two tables
> 
>   I have a table called Jobs and another called Applications. I can
> query all Job rows, but how do I also include the number of
Application
> rows for each Job row in the SELECT statement?
> 
> applications has a jobid foreign key
> 
> create table job (      jobid SERIAL,
>                          login varchar(40),
>                          jobtitle varchar(70),
>                          jobdescrip TEXT,
>                          jobkeywords TEXT,
>                          valid int2 NOT NULL,
>                          postdate TIMESTAMPTZ,
>                          PRIMARY KEY (jobid),
>                          FOREIGN KEY(login) REFERENCES company
>                              ON DELETE CASCADE
>                          );
> 
> 
> CREATE TABLE application        (   applid SERIAL,
>                                      jobid INTEGER,
>                                      login varchar(6),
>                                      apldate TIMESTAMPTZ,
>                                      PRIMARY KEY (applid),
>                                      FOREIGN KEY (jobid) REFERENCES
job
>                                          ON DELETE CASCADE,
>                                      FOREIGN KEY (login) REFERENCES
> student
>                                          ON DELETE CASCADE
>                                  );
> 
> 

How about using a sub-select.

SELECT t1.*, (SELECT COUNT(*) FROM application s1 WHERE
s1.jobid=t1.jobid)  AS app_count FROM job t1;


Nick






^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-01-15 08:37 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-01-14 23:33 Selecting from two tables Michael Hanna <taojones@sympatico.ca>
2004-01-15 07:44 Re: Selecting from two tables mauro.folcarelli@sistinf.it
2004-01-15 08:37 Re: Selecting from two tables Nick Barr <nick.barr@webbased.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox