Retriving all records from left table and matching records from right

Hafsa Abdul gafoor

I am trying four queries to retrieve all records from Table1 and matching records from Table2 but my query returns only two records instead of three.

Table1:

EmpId name
1     xyz1
2     xyz2
3     xyz3

Table2:
EmpId   dateIn
1       2015-05-05
2       2015-05-05

Required result:

EmpId name   DateIn
1     xyz1   2015-05-05
2     xyz2   2015-05-05
3     xyz3   NULL

select Table1.EmpId, Table1.name, Table2.dateIn from Table1 left join Table2 on Table1.empid=Table2.empid where date_format(Datein, '%Y-%m-%d')='2015-05-05'

select Table1.EmpId, Table1.name, Table2.dateIn from Table1 left join Table2 on Table1.empid=Table2.empid where date_format(Datein, '%Y-%m-%d')='2015-05-05' group by Table1.EmpId, Table1.name, Table2.dateIn

select Table1.EmpId, Table1.name, Table2.dateIn from Table1,Table2 where Table1.empid=Table2.empid and date_format(Datein, '%Y-%m-%d')='2015-05-05' group by Table1.EmpId, Table1.name, Table2.dateIn

select Table1.EmpId, Table1.name, Table2.dateIn from Table1,Table2 where Table1.empid=Table2.empid and date_format(Datein, '%Y-%m-%d')='2015-05-05'

Please help.

Neeraj Sharma

This will work fine.
Sql Query

declare @Table1 as table( empid tinyint, name varchar(10))
insert into @table1 values (1,'xyz1')
insert into @table1 values (2,'xyz2')
insert into @table1 values (3,'xyz3')

declare @Table2 as table (Empid tinyint, datein date)
insert into @Table2 values(1,'2015-05-05')
insert into @Table2 values(2,'2015-05-05')


select Table1.EmpId, Table1.name, Table2.dateIn from @Table1 Table1 left join @Table2 Table2 on Table1.empid=Table2.empid and '2015-05-05'='2015-05-05'--(or date_format(Datein, '%Y-%m-%d')='2015-05-05')

result

EmpId name       dateIn
----- ---------- ----------
 1     xyz1       2015-05-05
 2     xyz2       2015-05-05
 3     xyz3       NULL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL returning all the records from the left table excluding the records that have a match in the right table

From Dev

Not getting all records from Table A in left join

From Dev

LEFT JOIN does not return all the records from the left side table

From Dev

SQL Query - select all from one table with matching records in another

From Dev

Linq - Inner Join not retrieving all records from left table

From Dev

Linq - Inner Join not retrieving all records from left table

From Dev

How to select full left table and where condition match records from right table?

From Dev

Select All Records From One Table And Matching Record From Other Table

From Dev

Retriving 3 last records from database for each id

From Dev

All records being deleted when deleting records from a temp table

From Dev

SQL server left join not returning expected records from left table

From Dev

Get all records from table - EclipseLink

From Dev

Get all records from azure table storage

From Dev

Getting all records from database to Vaadin Table

From Dev

Get all records from another table with pivot

From Dev

How to display all records from a table with RedBeanPHP?

From Dev

Delete all records from table that starts with <

From Dev

Retriving all records but still showing error in console

From Dev

Select rows from a table matching other table records

From Dev

Select rows from a table matching other table records

From Dev

Extract records from table

From Dev

Identifying matching records from tables

From Dev

Matching records from two files

From Dev

unable to get records from left join in 0:m relationship table

From Dev

unable to get records from left join in 0:m relationship table

From Dev

Left join and chose matching columns from right table only

From Dev

SQL: All records from one table, and all records from another, including null

From Dev

Get Records from Referenced Table "Minus" All The Records referenced from the Referencing Table

From Dev

How to get all records from one table and only records from joined table with criteria

Related Related

  1. 1

    MySQL returning all the records from the left table excluding the records that have a match in the right table

  2. 2

    Not getting all records from Table A in left join

  3. 3

    LEFT JOIN does not return all the records from the left side table

  4. 4

    SQL Query - select all from one table with matching records in another

  5. 5

    Linq - Inner Join not retrieving all records from left table

  6. 6

    Linq - Inner Join not retrieving all records from left table

  7. 7

    How to select full left table and where condition match records from right table?

  8. 8

    Select All Records From One Table And Matching Record From Other Table

  9. 9

    Retriving 3 last records from database for each id

  10. 10

    All records being deleted when deleting records from a temp table

  11. 11

    SQL server left join not returning expected records from left table

  12. 12

    Get all records from table - EclipseLink

  13. 13

    Get all records from azure table storage

  14. 14

    Getting all records from database to Vaadin Table

  15. 15

    Get all records from another table with pivot

  16. 16

    How to display all records from a table with RedBeanPHP?

  17. 17

    Delete all records from table that starts with <

  18. 18

    Retriving all records but still showing error in console

  19. 19

    Select rows from a table matching other table records

  20. 20

    Select rows from a table matching other table records

  21. 21

    Extract records from table

  22. 22

    Identifying matching records from tables

  23. 23

    Matching records from two files

  24. 24

    unable to get records from left join in 0:m relationship table

  25. 25

    unable to get records from left join in 0:m relationship table

  26. 26

    Left join and chose matching columns from right table only

  27. 27

    SQL: All records from one table, and all records from another, including null

  28. 28

    Get Records from Referenced Table "Minus" All The Records referenced from the Referencing Table

  29. 29

    How to get all records from one table and only records from joined table with criteria

HotTag

Archive