Solved

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

Posted on 2011-09-30
4
321 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
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

726 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