mercredi 17 octobre 2018

Joining the table creating a duplicate entry in laravel

I just want to merge the content of 2 tables and display it based on the ID.

Both the table has 3 entries.

Table a - Sampling order

Date Docname Products Quantity ID
1     A       A         1      1
2     B       B         2      1 
3     C       C         3      1

Table B - Representative locations

Date Area lat long ID
 1    a    1   1   1
 2    b    2   2   1
 3    c    3   3   1

The output should generate like 3 rows with all the table A columns and B columns where ID = Specified ID

But its generating 9 rows (3*3) and duplicating the numbers of rows present in both the tables. Its generating

A
B
C
A
B
C   
A
B
C

Combing number of rows in A * B - I just need only 3 rows with respect to ID.

Query -

$Report = DB::table('sampling_order')
             ->join('representativelocations','representativelocations.representativeid','=','sampling_order.representativeid')
             ->select('sampling_order.representativeid as representativeid',
             'sampling_order.date as date',
             'sampling_order.doctor_name as doctor_name',
             'sampling_order.products as products',
             'sampling_order.quantity as quantity',
             'representativelocations.latitude as latitude',
             'representativelocations.longitude as longitude',
             'representativelocations.area as area')
             ->whereBetween('sampling_order.date', [$Datefrom, $Dateto])
             ->where('sampling_order.representativeid','=',$Representativeid)->get();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire