SQL syntax query

Hi there,

I'm using a query to pull information from three different tables. Its fairly simple to do this other than the fact that from one table I want to pull data from the recordwith the minimum ID value.

I basically want to combine the two queries shown in the code. The field "t.surname" in the first query should really be filled with the result of the second query


SELECT l.vch_code, t.surname,  q.* FROM suppfail.quote AS q INNER JOIN suppfail.policy AS p ON q.id = p.quote_id INNER JOIN ocs.logins AS l on q.brk_id = l.int_id
INNER JOIN suppfail.traveller AS t on q.id = t.quote_id
WHERE q.status = 'live' AND q.date_issue BETWEEN '2009-05-01' AND '2009-05-15'
 
SELECT surname FROM suppfail.traveller t WHERE quote_id = x AND id IN (SELECT MIN(ID) FROM suppfail.traveller t WHERE quote_id = x);

Open in new window

g-spotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
This is one method:
SELECT l.vch_code, t.surname,  q.* 
FROM suppfail.quote AS q 
INNER JOIN suppfail.policy AS p ON q.id = p.quote_id 
INNER JOIN ocs.logins AS l on q.brk_id = l.int_id
INNER JOIN suppfail.traveller AS t on q.id = t.quote_id
WHERE q.status = 'live' 
AND q.date_issue BETWEEN '2009-05-01' AND '2009-05-15'
AND t.id = (SELECT MIN(ID) FROM suppfail.traveller t2 WHERE t2.quote_id = t.quote_id);

Open in new window

0
Kevin CrossChief Technology OfficerCommented:

SELECT l.vch_code, t.surname,  q.* 
FROM suppfail.quote AS q 
INNER JOIN suppfail.policy AS p ON q.id = p.quote_id 
INNER JOIN ocs.logins AS l on q.brk_id = l.int_id
INNER JOIN (
   SELECT quote_id, MIN(ID) as id 
   FROM suppfail.traveller GROUP BY quote_id
) t_min on q.id = t_min.quote_id
INNER JOIN suppfail.traveller AS t on q.id = t.quote_id and t.id = t_min.id
WHERE q.status = 'live' 
AND q.date_issue BETWEEN '2009-05-01' AND '2009-05-15';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
g-spotAuthor Commented:
Thanks mwvisa1.

Thats great. Works perfectly. Cheers.
0
Kevin CrossChief Technology OfficerCommented:
Glad that helped!

Cheers,
Kevin
0
g-spotAuthor Commented:
Not only did it help but for the first time I actually understood how it worked and can make use of the code again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.