accessing different columns with the same name when joining tables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    #1

    accessing different columns with the same name when joining tables

    hi,

    i have two tables that i am joining using the following sql:

    SELECT * FROM clients, programmes WHERE clients.client_ id =
    programmes.clie nt_id AND programmes.clie nt_id = 12


    if both clients table and programmes table contain a column called
    date_added, how do i access them both seperately? i have tried the
    following:

    //after connect code etc.

    while ($row = mysql_fetch_arr ay($result)) {
    $clients_date_a dded = $row['clients.date_a dded'];
    }


    ....but it doesnt seem to work. any help?

    cheers

    burnsy
  • Marcin Dobrucki

    #2
    Re: accessing different columns with the same name when joining tables

    mr_burns wrote:
    [color=blue]
    > i have two tables that i am joining using the following sql:
    >
    > SELECT * FROM clients, programmes WHERE clients.client_ id =
    > programmes.clie nt_id AND programmes.clie nt_id = 12
    >
    >
    > if both clients table and programmes table contain a column called
    > date_added, how do i access them both seperately? i have tried the
    > following:
    >
    > //after connect code etc.
    >
    > while ($row = mysql_fetch_arr ay($result)) {
    > $clients_date_a dded = $row['clients.date_a dded'];
    > }
    >
    > ...but it doesnt seem to work. any help?[/color]

    There is going to be a collision, instead, use something like this:

    SELECT clients.date_ad ded as c_date_added, programmes.date _added as
    p_date_added FROM ...

    /marcin

    Comment

    • Phil Roberts

      #3
      Re: accessing different columns with the same name when joining tables

      bissatch@yahoo. co.uk (mr_burns) emerged reluctantly from the
      curtain and staggered drunkenly up to the mic. In a cracked and
      slurred voice he muttered:

      <snip>[color=blue]
      > ...but it doesnt seem to work. any help?[/color]

      SELECT c.date_added AS clients_date_ad ded,
      p.date_added AS programmes_date _added
      FROM clients c, programmes p
      WHERE c.client_id = p.client_id
      AND p.client_id = 12

      while ($row = mysql_fetch_arr ay($result)) {
      echo $row['clients_date_a dded'];
      echo $row['programmes_dat e_added'];
      }

      You were almost there. However if you wan't to select any more
      columns you'll need to explicitly name them.

      --
      Phil Roberts | Deedle Doot Doo Dee Dee | http://www.flatnet.net/

      I could be wrong here, You could be right
      Please forgive me I have sinned - Not on your life
      But that's how you want me, But I'll never fear thee
      Why you and not me? Tell me Holy Man

      Comment

      Working...