Garayed.com  

Go Back   Garayed.com > mySQL
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-2008, 06:08 PM
mcardeiro@yahoo.com
 
Posts: n/a
Default return multiple records from join as single row

Hi

I can't seem to get my head around this join. I basically want to
select 2 records from the join as a row. let me explain.

table people: id|name;
table weights: id|week|weight;

i want to select name from people and join weights where id matches,
but I want 2 records from weights to be returned in a single row...
where weights.week=1 and weights.week=4

so if my db had the following tables

-----------
people:
----------
id = 1,
name = mike;

---------
weights
---------
id=1,
week=1,
weight=100;
...........
id=1,
week=4,
weight=120;


i would want the select to return as a single row:
mike, 100, 120


I solved it programmatically with multiple selects, but I'm sure there
is a much more elegant solution

Thanks

Mike Cardeiro
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-13-2008, 06:11 PM
Rik Wasmus
 
Posts: n/a
Default Re: return multiple records from join as single row

On Fri, 13 Jun 2008 20:08:08 +0200, mcardeiro@yahoo.com
<mcardeiro@yahoo.com> wrote:

> Hi
>
> I can't seem to get my head around this join. I basically want to
> select 2 records from the join as a row. let me explain.
>
> table people: id|name;
> table weights: id|week|weight;
>
> i want to select name from people and join weights where id matches,
> but I want 2 records from weights to be returned in a single row...
> where weights.week=1 and weights.week=4
>
> so if my db had the following tables
>
> -----------
> people:
> ----------
> id = 1,
> name = mike;
>
> ---------
> weights
> ---------
> id=1,
> week=1,
> weight=100;
> ..........
> id=1,
> week=4,
> weight=120;
>
>
> i would want the select to return as a single row:
> mike, 100, 120
>
>
> I solved it programmatically with multiple selects, but I'm sure there
> is a much more elegant solution


This is just a basic join?

SELECT p.name, w1.weight, w2.weight
FROM people p
JOIN weights w1
ON w1.id = p.id
AND w1.week = 1
JOIN weights w2
ON w2.id = p.id
AND w2.week = 1
WHERE p.name LIKE 'mike'

Then again, dieting is bad for the mind :P
--
Rik Wasmus
....spamrun finished
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:09 AM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.