【发布时间】:2019-03-22 12:06:00
【问题描述】:
我只是想将2个表格的内容合并起来,根据ID显示出来。
两个表都有 3 个条目。
表 a - 抽样顺序
Date Docname Products Quantity ID
1 A A 1 1
2 B B 2 1
3 C C 3 1
表 B - 代表地点
Date Area lat long ID
1 a 1 1 1
2 b 2 2 1
3 c 3 3 1
输出应该生成 3 行,其中 ID = 指定 ID 的所有表 A 列和 B 列
我需要这样的输出
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
但它生成 9 行 (3*3) 并复制两个表中存在的行数。 它的产生
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
结合 A * B 中的行数 - 我只需要 3 行相对于 ID。
查询 -
$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();
【问题讨论】:
-
似乎连接(代表位置)表存在问题。截至目前,表 sampling_order 包含 3 条记录。第二个表包含 3 条记录。我只想根据 id 合并两者。
标签: mysql sql database laravel laravel-5