【问题标题】:Joining the table creating a duplicate entry in laravel加入表格在 laravel 中创建重复条目
【发布时间】: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


【解决方案1】:

根据要求,我认为您应该尝试加入date 而不是ID,如下所示。这将按照您的预期返回 3 行。

select so.date,so.Docname,so.products,so.Quantity,rl.Area,rl.lat,rl.long from Sampling_order AS so
INNER JOIN  Representative_locations as rl ON so.Date = rl.Date
WHERE so.ID = 1

根据需要更改表名和列名。

【讨论】:

  • 你使用 distinct 吗?
  • distinct 现在返回 6 行
  • 您能否发布准确的预期输出以及包含数据类型和 PK 的表结构?
  • 编辑所需输出并生成输出。
【解决方案2】:

我可以看到,两个表中的所有 Id 都是“1”, 所以结果应该是 3 * 3 = 9 行。 我猜你可能想要这个:

id产品区-A区-B区-C

1____A____1____2_____3

1____B____2____3_____4

如果是这样。您需要参加 3 次。

SELECT * FROM A
LEFT JOIN (SELECT *, area AS area-A FROM B WHERE area = 'a' ) AS B ON B.id = A.id
LEFT JOIN (SELECT *, area AS area-B FROM B WHERE area = 'b' ) AS C ON C.id = A.id
LEFT JOIN (SELECT *, area AS area-C FROM B WHERE area = 'c' ) AS D ON D.id = A.id

希望这是你想要的。

【讨论】:

  • 如果 2 个表中存在 4 个条目,则返回 16 行。 3 只是一个例子。
  • 我想要这两张表中的日期文档名称产品数量经纬度区域。我只想合并关于 ID 的数据
猜你喜欢
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2019-01-18
  • 2023-03-27
  • 2019-12-29
相关资源
最近更新 更多