?
Solved

INNER JOIN WITH SAME PRIMARY ID

Posted on 2013-12-24
3
Medium Priority
?
409 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
[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
3 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 440 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

The Ideal Solution for Multi-Display Applications

Check out ATEN’s VS1912 12-Port DP Video Wall Media Player at InfoComm 2017. Kerri describes how easy it is to design creative video walls in asymmetric layouts and schedule detailed playlists ahead of time with its advanced scheduling feature.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 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