Search code examples
javamysqlsqlanywhere

How to select particular range of values from joining two tables in mysql?


I have two tables and I have to select the values from two tables based on the query

I have two tables like tab1,tab2 and each having same columns name,age,job,gender. and in tab 2 I have extra column place

I have to select all values from tab1 and tab2 where the gender is male

how to do it?

SELECT tab1.name,tab1.gender,tab1.age,tab2.place FROM tab1 INNER JOIN tab2 ON tab1.gender=mhdetail.gender where gender='male';

is this correct?


Solution

  • You're not looking for a join. You want an Union:

    SELECT *
    FROM MyTable1
    WHERE Gender = 'Male'
    
    UNION -- Or 'UNION ALL' if you want duplicates.
    
    SELECT *
    FROM MyTable2
    WHERE Gender = 'Male'
    

    A join would be used if you had 2 tables, and wanted info from the 2 tables, but only one table would contain the gender:

    SELECT *
    FROM   MyTable1
    JOIN   MyTable2 ON MyTable1.ID = MyTable1.ID
    WHERE  MyTable1.Gender = 'Male'