Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Combining info from two tables?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-18-04, 13:01
spacdude spacdude is offline
Registered User
 
Join Date: Sep 2004
Location: UK
Posts: 15
Combining info from two tables?

Hello, I'm having some problems trying to access two tables in a SQL database at the same time and making some results out of them. Let me explain further: the first table has some information in that I'm going to be doing a select query on and reading out, but one of the columns in this table is a set of codes, the second table contains the codes in one column and their meanings in the other.

So I want to bring back the information from the first table and then select the information for the codes shown from the second table and print their meanings alongside the information from the first table. Could anyone help me out in figuring out how my SQL in the ASP page for this would be written? Sorry if this is a little confusing but im having a hard time visualising how to do this.
Reply With Quote
  #2 (permalink)  
Old 10-18-04, 13:06
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Your query will look something like this:

Code:
select t1.col1, t1.col2, t1.col3, t1.code, t2.description from t1 join t2 on t1.code = t2.code where ...;
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 10-19-04, 10:54
spacdude spacdude is offline
Registered User
 
Join Date: Sep 2004
Location: UK
Posts: 15
Cheers, it works. I've only been using very basic SQl views in the past so joins etc are new to me. I do have another question tho, I'm writing these queries in an ASP page and they are very long single lines at the mo, how do I break them up into seperate shorter lines?
Reply With Quote
  #4 (permalink)  
Old 10-19-04, 11:03
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Something like this, if I recall correctly:
Code:
strSQL = "select empno" _ & " from emp" _ & " where ename = ?"
i.e. the underscore is the "continued on next line" marker for VBScript.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 10-19-04, 11:13
spacdude spacdude is offline
Registered User
 
Join Date: Sep 2004
Location: UK
Posts: 15
Thanks again for you help andrew
Reply With Quote
  #6 (permalink)  
Old 10-19-04, 13:22
spacdude spacdude is offline
Registered User
 
Join Date: Sep 2004
Location: UK
Posts: 15
Question

Ok i've hit another snag with this, there are two different columns in the first table that are codes that need to be compared to the code listing in the 2nd table and then their code meanings sent back. I ve done this with one which is where u're first piece of code comes in handy but i can't join two columns in the 1st table to the one in the 2nd.
Reply With Quote
  #7 (permalink)  
Old 10-19-04, 13:35
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Oh no, not the "One True Look-up Table"?

You can join to the same table twice like this:
Code:
select t1.col1, t1.col2, t1.col3, t1.code1, t2_1.description, t1.code2, t2_2.description from t1 join t2 t2_1 on t1.code1 = t2_1.code join t2 t2_2 on t1.code2 = t2_2.code where ...;
t2_1 and t2_2 are "aliases" for the table t2 which now appears twice in the query.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #8 (permalink)  
Old 10-19-04, 14:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,539
that's funny, i took "one of the columns in this table is a set of codes" to mean something completely different (a denormalized table)

looks like you may have understood the requirements better than i did, tony

see http://forums.devshed.com/t193376/s.html
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #9 (permalink)  
Old 10-19-04, 15:45
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Who knows? Maybe you are right - it's equally possible!
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #10 (permalink)  
Old 10-20-04, 06:34
spacdude spacdude is offline
Registered User
 
Join Date: Sep 2004
Location: UK
Posts: 15
Sorry guys I think i'm over complicating the matter, let me try to explain it in a minimal way,

Table 1 with 3 columns, we'll call it date, the next column we will call code1 and the last column we will call code2.

Table 2 has 2 columns, one called error_codes and another called translation with is the text meaning of the error codes.

I want the query to select all the records in the first table for a certain criteria and give the translations for the code columns in the first table from the 2nd table.
Reply With Quote
  #11 (permalink)  
Old 10-20-04, 07:16
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Well, I think I have already given you the SQL for that above.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #12 (permalink)  
Old 10-20-04, 10:50
spacdude spacdude is offline
Registered User
 
Join Date: Sep 2004
Location: UK
Posts: 15
I've actually found that the 2nd column in the first table referred to a different set of codes in another table so the join will work now, thank you both for your help, i'm learning it bit by bit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On