0

I have 3 tables, I need advice on how to get data from them.

Table name

details_varchar  

details_int  

details_date

each of them have value_id[Int] and value_name [value_name type varies as per table name - Varchar, Int, Date]

Conditions

  • only one unique value_id will be present in any one of the 3 tables. i.e ID from 1 to n are stored in these tables according to their Data Types. If value_id = 1 and value_name[type] = int then it stores in details_int table.

What I need

select value_name from 3 tables[may appear in any one of the table] where value_id = '[I have this value stored in a variable]'

What I tried

UNION - but all the table needs to have same column type.

JOINS - All the fields should be connected.[I guess]

1 Answer 1

1
SELECT value_name
FROM (  SELECT value_name
        FROM details_varchar
        WHERE value_id = {$int_value_id}
        UNION ALL
        SELECT value_name
        FROM details_int
        WHERE value_id = {$int_value_id}
        UNION ALL
        SELECT value_name
        FROM details_date
        WHERE value_id = {$int_value_id}) AS h

This should give you what you want?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.