I have below tables
In x_table
, I have different records. I want to fetch all currencies from x_table
where continent is Asia
which is straight forward as below,
- SELECT currency from x_table where continent=’Asia’
and it should return Rupee
and Yen
rows which is also fine.
Now look at type
columns in x_table
and then another y_table
table. type
value represents different columns in y_table
Now query should be (considering two tables)
Fetch all currencies from x_table
where continent
is something
BUT check relative type
column in y_table
. If respective type
column value is 1 then and then fetch the record otherwise ignore it.
something like
SELECT continent, currency FROM x_table as X inner join y_table as Y on X.continent = Y.continent (BUT check if matching "type" column value is 1) if it is 0 ignore it.
With this logic, if you consider 1. query again, it should return only Rupee
row because Rupee_Dual
in y_table
for Asia
cotinent is 1
.
But Yen
row should not return because Yen_Single
in y_table
for Asia
continent is 0
.
2
Answers
Mapping a value to a column name is not inherent part of the relational algebra of SQL. Meaning: better not done.
Instead make an other table instead y_table
y_table’
A column
value
probably not needed.Then the SQL is no problem.
https://dbfiddle.uk/ce9Q4NEX