Welcome to WebmasterWorld Guest from 23.20.6.115

Forum Moderators: open

Message Too Old, No Replies

Selecting multiple rows from a single table

     
1:38 am on Oct 28, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Hello All -

I've been trying various ways of selecting 2 rows of data from the same table, but nothing I do is working.

The only way I've got this to work is by doing 2 separate selects on this one table and then run queries on both selects (yuck!). I know there must be a way to do it (some kind of row join on the table?) but I can't get the syntax right.

The table contains 3 columns: fld_type, fld_04_hrs, and fld_08_hrs. What I want to do is get the 4 hour and 8 hour data for types 'Kayak - Single' AND 'Kayak - Double'.

This doesn't work, but sort of like:

$sql =
"SELECT fld_04_hrs, fld_08_hrs
FROM tbl_activities
WHERE fld_type = 'Kayak - Single' AND fld_type = 'Kayak - Double'";

The 2 selects that work are:

$sql =
"SELECT fld_04_hrs, fld_08_hrs
FROM tbl_activities
WHERE fld_type = 'Kayak - Single'";

...AND...

$sql =
"SELECT fld_04_hrs, fld_08_hrs
FROM tbl_activities
WHERE fld_type = 'Kayak - Double'";

But I'd really like to grab both pieces of data for both types in one select.

Can someone please show me how to do this?

Great appreciation in advance for anyone who can show me how this is done.
1:44 am on Oct 28, 2012 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


$sql =
"SELECT fld_04_hrs, fld_08_hrs
FROM tbl_activities
WHERE fld_type = 'Kayak - Single' OR fld_type = 'Kayak - Double'";

Your initial query is saying match both (by using AND) which is why no rows are returned. Use OR instead.
1:58 am on Oct 28, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Hello brotherhood of LAN -

Thanks for the fast reply.

I tried that select yesterday and I do get results but I only get 2 results (for Kayak - Single), not the 4 results I need (2 for kayak single and 2 for kayak double).

Indeed, I do need to match both... I think it can be solved with a self-join (or by some other means) but, like mentioned previously, I just can't seem to get the syntax correct.
7:03 pm on Oct 28, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2002
posts:3196
votes: 12


brotherhood of LAN is correct, you must be making another error somewhere.