Umesh Moghariya
Umesh Moghariya

Reputation: 3183

MySql Query Optimisation

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

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]

Upvotes: 0

Views: 40

Answers (1)

Robin Castlin
Robin Castlin

Reputation: 10996

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?

Upvotes: 1

Related Questions