Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hosting images 4 28
MySQL Error Code 2 19
Responsive Design or Mobile Sites? 4 31
URGENT - Wordpress site missing  theme 9 41
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

770 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