How can I use Logical Operator between 2 Select Statements?

Viewed 171

I have few tables which are joined by some conditions.

What I am trying to achieve is Combine 2 SELECT statements in such way where

If there is data in Condition 1 display them OR go to Condition 2 and display data

Condition 1 - I am getting record from table say for exampleA, B, C and D based on some conditions

Condition 2 - I am getting record from table say for example A, B, C and E based on some conditions

What is am trying to achieve is

Display record if it exists in Condition 1

OR

Display record if it exits in Condition  2

Condition 1/ Query 1 - Display data

async getData() {
        try {
            const data = await this._conn.query(`

select first_name.value_name,quiz_table.answer, windows,player,first_name.value_id,country_place,current_name, pet_name, marker, relations
from schema_name.plugin,schema_name.quiz_table,schema_name.first_name, schema_name.value_version, schema_name.relationss
where plugin.answer= quiz_table.answer
and quiz_table.windows=first_name.value_id
and marker is not null
and schema_name.value_version.value_id= schema_name.first_name.value_id
and schema_name.value_version.caste= schema_name.first_name.caste
and schema_name.value_version.value_name= schema_name.first_name.value_name
and schema_name.value_version.version_number= schema_name.first_name.version_number
and schema_name.relationss.value_id= schema_name.first_name.value_id
and schema_name.relationss.caste= schema_name.first_name.caste
and schema_name.relationss.value_name= schema_name.first_name.value_name
and schema_name.relationss.version_number= schema_name.first_name.version_number
and  schema_name.quiz_table.windows= schema_name.first_name.value_id
and in_process='N'
}

OR

Condition 2/ Query 2 - Display data

select schema_name.relationss."relations", schema_name.quiz_table."answer", schema_name.quiz_table."windows", schema_name.quiz_table."in_process", schema_name.quiz_table."object_name", schema_name.quiz_table."processed_date", schema_name.quiz_table."player", schema_name.quiz_table."country_place", schema_name.tools."mesh_scope_note", schema_name.plugin."current_name", schema_name.plugin."pet_name"
            from schema_name.quiz_table, schema_name.tools, schema_name.plugin, schema_name.relationss, schema_name.value_version 
            where (in_process = 'N' 
              and schema_name.quiz_table."windows" = schema_name.tools."value_id"
              and schema_name.quiz_table."player" = schema_name.tools."language"
              and schema_name.quiz_table."answer" = schema_name.plugin."answer"
              and schema_name.relationss."language" = schema_name.quiz_table."player"
              and schema_name.relationss."language" = schema_name.tools."language"
              and schema_name.relationss."caste" = schema_name.tools."caste"
              and schema_name.relationss."value_name" = schema_name.tools."value_name"
              and schema_name.relationss."version_number" = schema_name.tools."version_number"
              and schema_name.relationss."value_id" = schema_name.tools."value_id"
              and schema_name.value_version."value_id" = schema_name.tools."value_id"
              and schema_name.value_version."version_number" = schema_name.tools."version_number"
              and schema_name.value_version."caste" = schema_name.tools."caste"
)

NOTE - 1-> I cannot use function or procedure here.

       2-> Both the `Conditions` contains `different data` 
1 Answers

The problem is, how will the computer choose which option to go to? A computer does not choose, it can't. You have to provide some metric for which to choose. The boolean operator || (or) is for checking if either is true (declarative-sorta), not for imperative. You can 'ask' "is a or b true?" but you can't tell a computer, "do this or that" without any weight to either one which would explain to the computer which to pick and when.

What would be possible is asking,

"If there is data in Condition 1 display them; if not, go to Condition 2 and display data".

If that suits your needs, this is how it could be coded (this is just the framework).

if (data1 != null) {
    show data1
}
else {
   show data2
}

Or your data could be non-null, but have no contents, in which you could try to get some data from it (say, getChildren() (totally random, I'm just making that up):

if (data1.getChildren() != null) {
    show data1
}
else {
    show data2
}

I hope you know that this is just pseudocode for the theory of it. show_ is not actual code, it's just the placeholder for whatever you would write. Would this work - checking if it is null and/or checking if its contents are null (i.e., it's empty/without data) and if so then going to the next dataset?

I don't know sql, but I thought that idea of check-null/check-contents-null might help.

Related