Solved

INNER JOIN WITH SAME PRIMARY ID

Posted on 2013-12-24
3
401 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:
Kent Olsen 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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

820 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