Solved

INNER JOIN WITH SAME PRIMARY ID

Posted on 2013-12-24
3
390 Views
Last Modified: 2013-12-26
I have 2 tables whit  this attributes
   TABLE A = person --> id | firstname  | lastname     |   jobtitle
   TABLE B = contact--> id | id_person  | description  | secondlastname
 
This is my query in mysql, operating correctly

$query_services = mysql_query("SELECT A.*,B.* FROM person A LEFT JOIN contact B ON A.id=B.person_id WHERE $con ORDER BY A.id ");

while ($row_services = mysql_fetch_array($query_services)) {
$id=$row_services['id'];
$nam=$row_services['firstname'];
$last=$row_services['lastname'];
$seclast=$row_services['secondlastname'];

MY PROBLEM IS
I WANT TO GET THE 'id' = $id OF TABLE 'person'
BUT, I get 'id' the table 'contact'. (I do not want that record)

should I do?
0
Comment
Question by:pixegoo
3 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 220 total points
ID: 39738385
Hi pixegoo,

The query returns two columns labelled 'id' and you want a specific one.  Instead of selecting A.*, B.*, just select the columns that you want.

$query_services = mysql_query("SELECT A.*,B.id_person,m B.description, B.secondlastname FROM person A LEFT JOIN contact B ON A.id=B.person_id WHERE $con ORDER BY A.id ");

Open in new window


Good Luck,
Kent
0
 
LVL 13

Expert Comment

by:magarity
ID: 39738388
Just itemize the fields you want instead of using A.* and B.*:  A.ID, A.firstname ... B.description, B.secondlastname
You do not need to SELECT fields in order to use them in JOIN or WHERE clauses. This will make sure you only get the ID from table A.
0
 
LVL 12

Expert Comment

by:North2Alaska
ID: 39738408
You can also specify an alias for the field.  

Select A.ID as PERSON_ID, B.ID as CONTACT_ID...

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now