For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > January 2008 > Re: [PHP-DB] A little SQL help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] A little SQL help
Evert Lammerts

2008-01-31, 4:15 am

Not completely sure what type of result you expect, but here's one that makes sense to me.

SELECT tour.record_id, tour.event_start_date, tour.event_end_date, tour.event_name, angler_results.result
FROM tour
LEFT JOIN angler_results
ON angler_results.tour_id = tour.record_id
AND angler_results.angler_id = 1

I've taken out the CASE - I personally never worked with that and I'd probably put a default value of 'N/A' in the column angler_results.result. The DISTINCT has to go too, I'm guessing that the relation tour.record_id -> angler_results.tour_id is 1 -> *
. The LEFT join CAN stay (as far as I know OUTER is only necessary when using ODBC or for maintaining compatibility with it, and then I'm still not sure what it does), if you want to have all rows in tour regardless of the join condition with NULL values
for angler_results.result where tour.record_id is not in angler_results.tour_id. To leave out the NULL values use an INNER JOIN instead.

Evert


Bastien Koert wrote:
> Hi All,
>
> Got myself stuck in a little sql here and can't seem to work out what I am doing wrong
>
> SELECT
> DISTINCT (tour.record_id), tour.event_start_date, tour.event_end_date, tour.event_name,CASE WHEN result is NULLTHEN 'N/A'ELSE angler_results.resultEND CASE
> FROM
> tourLEFT OUTER JOIN angler_results
> ON angler_results.tour_id = tour.record_idWHERE angler_results.angler_id =1
>
> where the table TOUR is as above in the primary part of the select and table ANGLER_RESULTS is (record_id, tour_id, angler_id, result)
>
>
> Any ideas?
>
> Bastien
>
>
>
> ________________________________________
_________________________
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com