Between two tables how does one SELECT the table where the id of a specific value exists mysql (duplicate)

Cornelius

I have asked this question before here: Between two tables how does one SELECT the table where the id of a specific value exists mysql however I feel that I didn't phrase it well enough.

I have 2 tables in my "Hiking" database lets say table 1 is called "Forest" and table 2 is called "Mountain". Both tables have a FOREIGN KEY "Trip_id" which is a PRIMARY KEY in table "Trip" (or something, this is a made up example) that is AUTO_INCREMENT. A trip can either be Mountain or Forest, so the 2 tables do not share any Trip_ids. They also each have an attribute that they do not share with the other table. Mountains has an attribute "Temperature" and Forests has an attribute "Atmosphere". What I want to do, is extract either "Temperature" or "Atmosphere" depending on which, Mountains or Forests contains the Trip_id value 74.

SELECT Temperature FROM Mountain WHERE Trip_id = 74 OR 
SELECT Atmosphere FROM Forest WHERE Trip_id = 74;

(I know the code above does not work).

I did end up solving this problem using Java:

String sqlStatement = "SELECT Trip_id FROM Mountains WHERE Trip_id = 74";

if (/*(execute sql statement) == null*/){
    //Use Forest (and Atmosphere)
}

else{
    //Use Mountain (and Temperature)
}

However there are no if statements in mysql, so I was wondering if it at all was possible to solve this using mysql.

What I was thinking was something like this: SELECT * FROM FOREST OR MOUNTAIN WHERE Trip_id = 74 EXISTS; (I know this code is rubbish and completely wrong but I hope it helps illustrate what I am aiming for).

P.S if the columns were the same, then this would be the best answer:

select m.*
from mountains m
where m.trip_id = 74
union all
select f.*
from forests f
where f.trip_id = 74;

Thanks to Gordon Linoff for providing that answer.

Sam

There ARE if statements in mysql. Anyway!

You can do this:

SELECT Temperature as Value FROM Mountain WHERE Trip_id = 74
Union All
SELECT Atmosphere as Value FROM Forest WHERE Trip_id = 74

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

MySql - How to select MAX ID from two tables with WHERE clause

From Dev

Select from one table where id (from another table) exists

From Dev

MySQL check if value already exists between two tables

From Dev

MySQL - SELECT all from TABLE_1 where id does not exist in two fields of TABLE_2

From Dev

How to select items from a table based on one value if another value does not exists? (eloquent/sql)

From Dev

Select from two tables Where not exists

From Dev

Select from two tables Where not exists

From Dev

How do a join three tables where one table doesn't match the ID of the other two?

From Dev

MySQL - How to select rows in a table where id value is in a comma delimited field in another table?

From Dev

Add one table's id to another table for specific value mysql

From Dev

MySQL: Select value where date between two dates. If date/value does not exist display date and value 0

From Dev

How to transfer data between tables, only where id does not exist

From Dev

How to transfer data between tables, only where id does not exist

From Dev

MySQL select id where at least one value not in list

From Dev

MySQL select id where at least one value not in list

From Dev

select row from one table where value exists from array from another table

From Dev

MySQL using an anti-join to select non duplicate values between two tables

From Dev

MySQL select one value from table if there are two languages

From Dev

How to return data from two tables only when the column value in one table is same as another table using MySQL?

From Dev

How to select elements between two specific table rows with XPath

From Dev

select where id == value and id != value mysql

From Dev

select where id == value and id != value mysql

From Dev

How to check if specified id does not exist in two tables in MySQL?

From Dev

SQL query to select from one table where either not in another table OR in that table with a specific value

From Dev

MySQL query to find if a value of one column in one table is between two values in two columns on another table

From Dev

select uncommon records between two tables - mysql

From Dev

MySQL - one to many select from two tables

Related Related

  1. 1

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  2. 2

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  3. 3

    MySql - How to select MAX ID from two tables with WHERE clause

  4. 4

    Select from one table where id (from another table) exists

  5. 5

    MySQL check if value already exists between two tables

  6. 6

    MySQL - SELECT all from TABLE_1 where id does not exist in two fields of TABLE_2

  7. 7

    How to select items from a table based on one value if another value does not exists? (eloquent/sql)

  8. 8

    Select from two tables Where not exists

  9. 9

    Select from two tables Where not exists

  10. 10

    How do a join three tables where one table doesn't match the ID of the other two?

  11. 11

    MySQL - How to select rows in a table where id value is in a comma delimited field in another table?

  12. 12

    Add one table's id to another table for specific value mysql

  13. 13

    MySQL: Select value where date between two dates. If date/value does not exist display date and value 0

  14. 14

    How to transfer data between tables, only where id does not exist

  15. 15

    How to transfer data between tables, only where id does not exist

  16. 16

    MySQL select id where at least one value not in list

  17. 17

    MySQL select id where at least one value not in list

  18. 18

    select row from one table where value exists from array from another table

  19. 19

    MySQL using an anti-join to select non duplicate values between two tables

  20. 20

    MySQL select one value from table if there are two languages

  21. 21

    How to return data from two tables only when the column value in one table is same as another table using MySQL?

  22. 22

    How to select elements between two specific table rows with XPath

  23. 23

    select where id == value and id != value mysql

  24. 24

    select where id == value and id != value mysql

  25. 25

    How to check if specified id does not exist in two tables in MySQL?

  26. 26

    SQL query to select from one table where either not in another table OR in that table with a specific value

  27. 27

    MySQL query to find if a value of one column in one table is between two values in two columns on another table

  28. 28

    select uncommon records between two tables - mysql

  29. 29

    MySQL - one to many select from two tables

HotTag

Archive