Linking two recordsets together

This is a discussion on Linking two recordsets together within the MySQL Database forums, part of the Database Forums category; How can you link two recordsets together so that the primary key used in one is from the other recordset. ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-31-2007
chris_huh
 
Posts: n/a
Default Linking two recordsets together

How can you link two recordsets together so that the primary key used
in one is from the other recordset. I need to have two repeat regions
(one within another). Both of the repeat regions are from recordsets
from the same table, just different queries in mysql.

My code looks like this now:

mysql_select_db($database_connBeetles, $connBeetles);
$query_rsBeetles = sprintf("SELECT id, date, habitat, replica,
trap_number, species, number_of_individuals,
COUNT(Number_of_individuals), SUM(Number_of_individuals),
COUNT(DISTINCT Species), date_format(date, '%%D %%M %%Y') as date2,
ref, ref2, week FROM data WHERE Habitat = %s GROUP BY ref ORDER BY ref
ASC", GetSQLValueString($colname_rsBeetles, "text"));
$rsBeetles = mysql_query($query_rsBeetles, $connBeetles) or
die(mysql_error());
$row_rsBeetles = mysql_fetch_assoc($rsBeetles);
$totalRows_rsBeetles = mysql_num_rows($rsBeetles);

mysql_select_db($database_connBeetles, $connBeetles);
$query_Recordset2 = "SELECT SUM(Number_of_individuals), Species, ref
FROM data WHERE ref = '".$row_rsBeetles['ref']."' GROUP BY species
ORDER BY species ASC";
$Recordset2 = mysql_query($query_Recordset2, $connBeetles) or
die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

The ref field is a combination of the habitat, date and replica
fields. Which separates the entries into the different datasets that
need to be queried. The second recordset (Recordset2) will need to be
repeated for each repetition of the higher recordset (rsBeetles), and
be linked so that the data for each of the Recordset2 repetitions is
the same data that is used for the rsBeetles recordset.

Is there any way to do this?

Reply With Quote
  #2 (permalink)  
Old 01-31-2007
Captain Paralytic
 
Posts: n/a
Default Re: Linking two recordsets together

On 31 Jan, 16:35, "chris_huh" <chris....@gmail.com> wrote:
> How can you link two recordsets together so that the primary key used
> in one is from the other recordset. I need to have two repeat regions
> (one within another). Both of the repeat regions are from recordsets
> from the same table, just different queries in mysql.
>
> My code looks like this now:
>
> mysql_select_db($database_connBeetles, $connBeetles);
> $query_rsBeetles = sprintf("SELECT id, date, habitat, replica,
> trap_number, species, number_of_individuals,
> COUNT(Number_of_individuals), SUM(Number_of_individuals),
> COUNT(DISTINCT Species), date_format(date, '%%D %%M %%Y') as date2,
> ref, ref2, week FROM data WHERE Habitat = %s GROUP BY ref ORDER BY ref
> ASC", GetSQLValueString($colname_rsBeetles, "text"));
> $rsBeetles = mysql_query($query_rsBeetles, $connBeetles) or
> die(mysql_error());
> $row_rsBeetles = mysql_fetch_assoc($rsBeetles);
> $totalRows_rsBeetles = mysql_num_rows($rsBeetles);
>
> mysql_select_db($database_connBeetles, $connBeetles);
> $query_Recordset2 = "SELECT SUM(Number_of_individuals), Species, ref
> FROM data WHERE ref = '".$row_rsBeetles['ref']."' GROUP BY species
> ORDER BY species ASC";
> $Recordset2 = mysql_query($query_Recordset2, $connBeetles) or
> die(mysql_error());
> $row_Recordset2 = mysql_fetch_assoc($Recordset2);
> $totalRows_Recordset2 = mysql_num_rows($Recordset2);
>
> The ref field is a combination of the habitat, date and replica
> fields. Which separates the entries into the different datasets that
> need to be queried. The second recordset (Recordset2) will need to be
> repeated for each repetition of the higher recordset (rsBeetles), and
> be linked so that the data for each of the Recordset2 repetitions is
> the same data that is used for the rsBeetles recordset.
>
> Is there any way to do this?


You need to look at JOIN, but more than that you SERIOUSLY need to
look at formatting your code!

Reply With Quote
  #3 (permalink)  
Old 01-31-2007
chris_huh
 
Posts: n/a
Default Re: Linking two recordsets together

On 31 Jan, 17:06, "Captain Paralytic" <paul_laut...@yahoo.com> wrote:
> On 31 Jan, 16:35, "chris_huh" <chris....@gmail.com> wrote:
>
>
>
> > How can you link two recordsets together so that the primary key used
> > in one is from the other recordset. I need to have two repeat regions
> > (one within another). Both of the repeat regions are from recordsets
> > from the same table, just different queries in mysql.

>
> > My code looks like this now:

>
> > mysql_select_db($database_connBeetles, $connBeetles);
> > $query_rsBeetles = sprintf("SELECT id, date, habitat, replica,
> > trap_number, species, number_of_individuals,
> > COUNT(Number_of_individuals), SUM(Number_of_individuals),
> > COUNT(DISTINCT Species), date_format(date, '%%D %%M %%Y') as date2,
> > ref, ref2, week FROM data WHERE Habitat = %s GROUP BY ref ORDER BY ref
> > ASC", GetSQLValueString($colname_rsBeetles, "text"));
> > $rsBeetles = mysql_query($query_rsBeetles, $connBeetles) or
> > die(mysql_error());
> > $row_rsBeetles = mysql_fetch_assoc($rsBeetles);
> > $totalRows_rsBeetles = mysql_num_rows($rsBeetles);

>
> > mysql_select_db($database_connBeetles, $connBeetles);
> > $query_Recordset2 = "SELECT SUM(Number_of_individuals), Species, ref
> > FROM data WHERE ref = '".$row_rsBeetles['ref']."' GROUP BY species
> > ORDER BY species ASC";
> > $Recordset2 = mysql_query($query_Recordset2, $connBeetles) or
> > die(mysql_error());
> > $row_Recordset2 = mysql_fetch_assoc($Recordset2);
> > $totalRows_Recordset2 = mysql_num_rows($Recordset2);

>
> > The ref field is a combination of the habitat, date and replica
> > fields. Which separates the entries into the different datasets that
> > need to be queried. The second recordset (Recordset2) will need to be
> > repeated for each repetition of the higher recordset (rsBeetles), and
> > be linked so that the data for each of the Recordset2 repetitions is
> > the same data that is used for the rsBeetles recordset.

>
> > Is there any way to do this?

>
> You need to look at JOIN, but more than that you SERIOUSLY need to
> look at formatting your code!


I thought JOIN was just for joining two tables together, can it be
used for two recordsets using the same table then?

Reply With Quote
  #4 (permalink)  
Old 01-31-2007
Captain Paralytic
 
Posts: n/a
Default Re: Linking two recordsets together

On 31 Jan, 17:09, "chris_huh" <chris....@gmail.com> wrote:
> On 31 Jan, 17:06, "Captain Paralytic" <paul_laut...@yahoo.com> wrote:
>
>
>
>
>
> > On 31 Jan, 16:35, "chris_huh" <chris....@gmail.com> wrote:

>
> > > How can you link two recordsets together so that the primary key used
> > > in one is from the other recordset. I need to have two repeat regions
> > > (one within another). Both of the repeat regions are from recordsets
> > > from the same table, just different queries in mysql.

>
> > > My code looks like this now:

>
> > > mysql_select_db($database_connBeetles, $connBeetles);
> > > $query_rsBeetles = sprintf("SELECT id, date, habitat, replica,
> > > trap_number, species, number_of_individuals,
> > > COUNT(Number_of_individuals), SUM(Number_of_individuals),
> > > COUNT(DISTINCT Species), date_format(date, '%%D %%M %%Y') as date2,
> > > ref, ref2, week FROM data WHERE Habitat = %s GROUP BY ref ORDER BY ref
> > > ASC", GetSQLValueString($colname_rsBeetles, "text"));
> > > $rsBeetles = mysql_query($query_rsBeetles, $connBeetles) or
> > > die(mysql_error());
> > > $row_rsBeetles = mysql_fetch_assoc($rsBeetles);
> > > $totalRows_rsBeetles = mysql_num_rows($rsBeetles);

>
> > > mysql_select_db($database_connBeetles, $connBeetles);
> > > $query_Recordset2 = "SELECT SUM(Number_of_individuals), Species, ref
> > > FROM data WHERE ref = '".$row_rsBeetles['ref']."' GROUP BY species
> > > ORDER BY species ASC";
> > > $Recordset2 = mysql_query($query_Recordset2, $connBeetles) or
> > > die(mysql_error());
> > > $row_Recordset2 = mysql_fetch_assoc($Recordset2);
> > > $totalRows_Recordset2 = mysql_num_rows($Recordset2);

>
> > > The ref field is a combination of the habitat, date and replica
> > > fields. Which separates the entries into the different datasets that
> > > need to be queried. The second recordset (Recordset2) will need to be
> > > repeated for each repetition of the higher recordset (rsBeetles), and
> > > be linked so that the data for each of the Recordset2 repetitions is
> > > the same data that is used for the rsBeetles recordset.

>
> > > Is there any way to do this?

>
> > You need to look at JOIN, but more than that you SERIOUSLY need to
> > look at formatting your code!

>
> I thought JOIN was just for joining two tables together, can it be
> used for two recordsets using the same table then?- Hide quoted text -
>
> - Show quoted text -


Read up on table aliases.
FROM data a
JOIN data b

a and b are treated as 2 completely separate tables that just happen
to contain exactly the same records.

Reply With Quote
  #5 (permalink)  
Old 01-31-2007
blaine@worldweb.com
 
Posts: n/a
Default Re: Linking two recordsets together

SELECT b.id,b.title,a.id,a.title
FROM article a JOIN article b ON a.id=b.id
LIMIT 10

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

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



All times are GMT +1. The time now is 09:25 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0