02-14-12, 06:15 #1Registered User
- Join Date
- Feb 2012
Unanswered: Querying multiple tables - and using brackets?
I desperately need to be able to query multiple tables, and have two questions around this both sort of relating to each other.
1) Two tables, TABLE1 and TABLE2. Both contain different data but have some columns that are the same and some entries within these columns are also the same (lets call one of these COLUMN1). I need to select all info from TABLE1 where the data in COLUMN1 in both tables is the same.
select * from table1
where table1.column1 = table2.column1
Would that work?...I'd then like to expand and do a similar thing with 3+ tables.
2) Then I'd like to be able to query table2 to do something basic like:
select * from TABLE2
where COLUMN3 = 'green' or 'red' or 'blue'
BUT from the result of that basic query, I'd like to be able to query it, to do what I'm asking in question 1. Is this where brackets come in?
Hope i've made sense, I've been trying to nail this for ages now and I'm sure it can be done. Would really appreciate any help/advice.
02-14-12, 06:46 #2Registered User
- Join Date
- Mar 2007
1) What happened when you tried? Why do you not introduce TABLE2 in FROM clause as you did with TABLE1?
However your requirements are quite vague to propose any query. It would be helpful if you posted a test case - DDL for involved tables, DML for representative data, expected result with description of rules leading to it. I am sure that after stating it, you will be able to find the required query yourself.
What shall be the result when there are multiple rows with the same COLUMN1 value in any/both tables?
What shall be the result when with "3+ tables", the same value of COLUMN1 is present in only 2 of them?
2) Not sure what you want. It seems that you should probably add a WHERE condition to the query "what I'm asking in question 1".
Consider using IN condition or multiple comparison conditions. For their detailed description, please consult in SQL Language Reference book. It is available with other Oracle documentation books e.g. online on http://tahiti.oracle.com/
Also I am not sure why are you concerning yourself with brackets. The samples you posted do not contain any. They may/shall be used in some places, but for correct query creation, just follow correct SQL syntax (stated in the book above).
02-14-12, 11:47 #3Registered User
- Join Date
- Jun 2003
- West Palm Beach, FL
In your SQL 101 course you must have learned about "JOIN"s...
This is the time to apply that knowledge.
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb