TransWikia.com

Oracle Select Query based on a column value

Database Administrators Asked on November 25, 2021

I have two tables and wanted to conditionally query one of the tables.
For example below are the two tables with few matching columns in two tables
Table A(SNO, Number,Name…)
Table B ( SNO, Number,FullName..)
Number column value is passed to the query dynamically.
Table A and B have matching Columns(SNO,Number)
I would like to run a query on table based on the value of Number column which is common in both tables.

Something like this
if number =12
Select Query on Table A
else
select Query on Table B

One Answer

You can use a union to select from two tables like follows:

SELECT *
FROM TABLE_A
WHERE number = 12
AND  {other conditions}
UNION
SELECT *
FROM TABLE_B
WHERE number != 12
AND  {other conditions}

Answered by Malcolm Byrne on November 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP