Solved

Using join to get records from two database tables, how to distinguish "id" fields?

Posted on 2011-09-30
4
323 Views
Last Modified: 2013-11-19
Gurus,
I am using left join to get records from two database tables, both having "id" (same name in both tables) as Primary index.
This is the SQL-sentence:
$SQL = " SELECT * FROM bryggplatser left join matrikel on bryggplatser.medlemsnr = matrikel.MedlemsNr";

And then it goes on:

$ret = mysql_query($SQL);
if (!$ret) { echo( mysql_error()); }
else {
while ($row = mysql_fetch_array($ret)) {
$id = $row["id"]; Here I'd like to have "id" from the table "bryggplatser", but I get id from the table "matrikel".
$bryggplats = $row["bryggplats"]; This one correctly comes from the table "bryggplatser"
$medlemsnr = $row["medlemsnr"]; This one correctly comes from the table "bryggplatser"
$bredd = $row["bredd"]; This one correctly comes from the table "bryggplatser"
$djup_bryggan = $row["djup_bryggan"]; This one correctly comes from the table "bryggplatser"
$djup_bojsten = $row["djup_bojsten"]; This one correctly comes from the table "bryggplatser"
$bojtyngd_mtrl = $row["bojtyngd_mtrl"]; This one correctly comes from the table "bryggplatser"
$bojtyngd_vikt = $row["bojtyngd_vikt"]; This one correctly comes from the table "bryggplatser"
$katting_lina = $row["katting_lina"]; This one correctly comes from the table "bryggplatser"
$godsdiameter_botten = $row["godsdiameter_botten"]; This one correctly comes from the table "bryggplatser"
$godsdiameter_ytan = $row["godsdiameter_ytan"]; This one correctly comes from the table "bryggplatser"
$boj_typ = $row["boj_typ"]; This one correctly comes from the table "bryggplatser"
$boj_farg = $row["boj_farg"]; This one correctly comes from the table "bryggplatser"
$anmarkning = $row["anmarkning"];  This one correctly comes from the table "bryggplatser"      
$fornamn = $row["Fornamn"];  This one correctly comes from the table "matrikel"
$efternamn = $row["Efternamn"];   This one correctly comes from the table "matrikel"

What is the correct way to get id from the table "bryggplatser" without changing any name?
0
Comment
Question by:lericson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 36893178
declare the columns in your query explicitly like:


SELECT bryggplatser.id, bryggplatser.bryggplats, .... etc FROM bryggplatser left join matrikel on bryggplatser.medlemsnr = matrikel.MedlemsNr
0
 
LVL 6

Expert Comment

by:Dangle79
ID: 36893186
have you tried defining $id as $row["bryggplatser.id"]  ??
0
 
LVL 41

Expert Comment

by:ralmada
ID: 36893189
And remember that it's never a good practice to use the "*"

because you not only encounter this kind of problem you're experiencing right now, but also you are slowing the query down by including unnecessary columns.
0
 

Author Closing Comment

by:lericson
ID: 36894816
Excellent! Thanks!
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question