gpt4 book ai didi

MySQL数据库设计+查询-Laravel

转载 作者:搜寻专家 更新时间:2023-10-30 23:32:29 24 4
gpt4 key购买 nike

我的数据库有点问题。

在我的应用程序中,我正在从 Excel 工作表中检索数据。我正在使用框架 Laravel 和库 Laravel Excel(maatwebsite)。

当我上传文件时。在此示例中,工作表中有 7 行被插入到数据库中。

数据库: enter image description here

我用这个查询从他们那里检索了 5 个:

  $documentData = DB::table('documents')
->select(array('weld', 'diameter', 'thicknes', 'steelgrade', 'material', 'weldingdate', 'welderid', 'requestid'))
->where('diameter', '21.3')
->where('thicknes', '2.5')
->where('steelgrade', '—Ú20')
->get();

我根据直径厚度钢级(我为每个 excel 模板的独特组合(可能有多种组合))选择数据。

我将这些信息放入匹配组合的excel文件中保存。一切正常。但是!当我上传另一个文件(excel 模板的相同唯一组合)时,除了一列外,信息几乎相同。

查询将检索所有行(2 次上传后 10 行)。我怎样才能确保它只会抓取最新上传的内容。我知道 Laravel 提供了 created_at、updated_at 列,但我想知道是否还有其他内容。排序依据/分组依据?或最新();

我想知道是否有人建议我如何解决这个问题。

最佳答案

您可以使用 groupBy 获取唯一值,然后使用 orderBy 创建_at,并使用 take() 获取最后插入的值。

DB::table('documents')
->select(array('weld', 'diameter', 'thicknes', 'steelgrade', 'material', 'weldingdate', 'welderid', 'requestid'))
->where('diameter', '21.3')
->where('thicknes', '2.5')
->where('steelgrade', '—Ú20')
->groupBy('weld')
->orderBy('created_at', 'desc')
->take(5); //Replace 5 for the count of the import

关于MySQL数据库设计+查询-Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47529748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com