LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2004, 10:16 PM   #1
Elijah
Member
 
Registered: Feb 2003
Location: Philippines
Distribution: Debian, Mandrake, Redhat
Posts: 90

Rep: Reputation: 15
Question [Postgresql] Combining queries?


I need to fetch data from two tables - organizationinfo and temp_organizationinfo,

Code:
select ctrl_no, organization_name, domain_name from organizationinfo, temp_organizationinfo where ctrl_no = '1420';
ERROR: Column reference "ctrl_no" is ambiguous

But I get the error above, how do I fetch from these two tables in one query??? another problem is that both tables have the same column names, so doing 'organizationinfo.ctrl_no' results in having me too choose between the two... but I want both tables.

Anyone help?
 
Old 08-09-2004, 12:09 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
SQL "join"

You need to perform a SQL "join" on your two tables. For example:

Code:
  select a.ctrl_no, organization_name, domain_name
    from organizationinfo a, temp_organizationinfo b
    where a.ctrl_no = b.ctrl_no
    and a.ctrl_no = '1420';
This does the following:
[list=1][*]Gives the aliases 'a' and 'b' to tabkes 'organizationinfo' and 'temp_organizationinfo'[*]Relates ("joins") table 'organizationinfo' to 'temp_organizationinfo" via the field 'ctrl_no'[*]Further restricts the query to only those records where 'ctrl_no' in 'organization_name' equals 1420[*]Prints out the fields ctrl_no, organization_name and domain_name for each record that matches your search criteria[/list=1]

I qualified "a.ctrl_no", to distinguish it from "ctrl_no" in the other table (temp_organizationinfo). If some of the other columns match, you'll have to qualify those column names, too.

If the same data is in both tables, I'm not sure why you'd even need a join: just do a simple "select" from one table or the other (but not both at the same time).

Finally, if you're planning on doing much database work, I'd *highly* recommend the following book:

"SQL Queries for Mere Mortals", Michael J. Hernandez and John L. Viescas

Hope that helps .. PSM
 
Old 08-09-2004, 12:28 AM   #3
Elijah
Member
 
Registered: Feb 2003
Location: Philippines
Distribution: Debian, Mandrake, Redhat
Posts: 90

Original Poster
Rep: Reputation: 15
Well, both tables has different data.. but they're the same in their columns.

Quote:
Hope that helps .. PSM
Yes, this helps a LOT! Thanks!
 
Old 08-09-2004, 11:14 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by Elijah
Well, both tables has different data.. but they're the same in their columns.
So if understand you well, you have two tables like this:
Code:
  Table "person1"
  +----------+---------+
  | name     | country |
  +----------+---------+
  | John     | NL      |
  | Benny    | DE      |
  | Robert   | UK      |
  +----------+---------+


  Table "person2"
  +----------+------+
  | nm       | ctry |
  +----------+------+
  | Richard  | US   |
  | Robert   | UK   |
  +----------+------+
And then you need as an answer to your SQL-query:
Code:
  +----------+---------+
  | John     | NL      |
  | Benny    | DE      |
  | Richard  | US      |
  | Robert   | UK      |
  +----------+---------+
If this is your question, then you need an "union", not a "join".
For the example above, your query would be:
Code:
  SELECT name, country
  FROM person1
  UNION DISTINCT
  SELECT nm, ctry
  FROM person2
Note that "DISTINCT" here means you don not want duplicate rows in the result. (like in this example, there's only one "Robert", "UK". If you do want duplicates, use "ALL" instead of "DISTINCT". Then you'll have two rows "Robert", "UK".

Last edited by Hko; 08-09-2004 at 11:15 AM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
combining pdfs RaI Linux - Software 1 09-09-2005 01:31 PM
Combining files into one cobra7x Linux - General 10 05-05-2005 01:39 AM
Write-combining thiagorobert Linux - Software 2 05-10-2004 03:50 PM
postgresql -odbc & postgresql-jdbc installation kjsubbu Linux - Software 0 06-19-2003 02:50 AM
Write Combining? TomGerman Linux - Software 1 11-30-2002 08:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration