18

I have one table with a column ID and SERVICE_TYPE_TEXT, and another table with columns

ID, SERVICE_TYPE ... 

and lots of other columns.

The SERVICE_TYPE in the second table contains the ID from the first table. I want to query so I can get the SERVICE_TYPE_TEXT from the first table that matches the ID given in the second table.

I've tried to join, and setting different names on ID with AS, but always at the end of the query result I get the original ID from the first table with column name ID, as well as the name I defined in the AS.

Any suggestions on how I can get the ID from the first table to stay away ? :)

3
  • 1
    Please post your select statement in the question, may be the part where you are selecting the column names Commented Oct 16, 2012 at 6:46
  • posting the actual query with sample table structure would definitly help here. Commented Oct 16, 2012 at 6:48
  • Maybe you should remove the * that is almost certainly somewhere in your SELECT clause (of course, this would be easier to diagnose if we could see your query) Commented Oct 16, 2012 at 6:49

3 Answers 3

21

Try something like this,

SELECT a.ID AS ServiceID,
       a.Service_Type_Text,
       b.ID AS table2ID,
       b.Service_Type
FROM   table1 a
       INNER JOIN table2 b
           ON a.ID = b.Service_Type
Sign up to request clarification or add additional context in comments.

2 Comments

what do you want for table2?
Thanks a lot! I personally didn't know you could assign columns like that.
14

Set your query so that it returns all data from the second table but only the required field (column) from the first.
Something like this:

SELECT TAB1.SERVICE_TYPE_TEXT, TAB2.*
FROM TAB1
INNER JOIN
TAB2
ON TAB1.ID = TAB2.SERVICE_TYPE

Comments

6

TRY

 SELECT a.ID AS ServiceID,
       a.Service_Type_Text,
       b.ID AS table2ID,
       b.Service_Type
FROM   table1 a
       INNER JOIN table2 b
           ON a.ID = b.Service_Type AND b.ID='YOUR_ID';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.