Solved

INNER JOIN WITH SAME PRIMARY ID

Posted on 2013-12-24
3
405 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 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

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

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…
What You Need to Know when Searching for a Webhost Provider
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

740 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