RE: [PHP-DB] A little SQL help
| From: | Bastien Koert | Date: | Thu, 31 Jan 2008 14:40:54 +0000 |
| Subject: | RE: [PHP-DB] A little SQL help | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-44550@lists.php.net to get a copy of this message | ||
since the results table is not fully populates, the left outer join is used to take all the records
from the tours table and include any matching records from the angler_results table. The CASE
statement is used to auto fill the result of the results for that particular record set where there
is no value from the angler_results table
Bastien> Date: Thu, 31 Jan 2008 10:16:23 +0100> From: evert.lammerts@gmail.com> To:
Bastien> bastien_k@hotmail.com> CC: php-db@lists.php.net> Subject: Re: [PHP-DB] A little SQL help>
Bastien> > Not completely sure what type of result you expect, but here's one that makes sense to
Bastien> me.> > SELECT tour.record_id, tour.event_start_date, tour.event_end_date, tour.event_name,
Bastien> angler_results.result> FROM tour> LEFT JOIN angler_results> ON angler_results.tour_id =
Bastien> tour.record_id> AND angler_results.angler_id = 1> > I've taken out the CASE - I
Bastien> personally never worked with that and I'd probably put a default value of 'N/A' in
Bastien> the column angler_results.result. The DISTINCT has to go too, I'm guessing that the relation
Bastien> tour.record_id -> angler_results.tour_id is 1 -> *. The LEFT join CAN stay (as far as I know
Bastien> OUTER is only necessary when using ODBC or for maintaining compatibility with it, and then I'm
Bastien> still not sure what it does), if you want to have all rows in tour regardless of the join condition
Bastien> with NULL values for angler_results.result where tour.record_id is not in angler_results.tour_id. To
Bastien> leave out the NULL values use an INNER JOIN instead.> > Evert> > > Bastien Koert
Bastien> wrote:> > Hi All,> > > > Got myself stuck in a little sql here and can't seem
Bastien> to work out what I am doing wrong> > > > SELECT > > DISTINCT (tour.record_id),
Bastien> tour.event_start_date, tour.event_end_date, tour.event_name,CASE WHEN result is NULLTHEN
Bastien> 'N/A'ELSE angler_results.resultEND CASE > > FROM > > tourLEFT OUTER JOIN
Bastien> angler_results > > ON angler_results.tour_id = tour.record_idWHERE angler_results.angler_id
Bastien> =1> > > > where the table TOUR is as above in the primary part of the select and table
Bastien> ANGLER_RESULTS is (record_id, tour_id, angler_id, result)> > > > > > Any
Bastien> ideas?> > > > Bastien> > > > > > > >
Bastien> _________________________________________________________________> >> >> > >
_________________________________________________________________