【问题标题】:Laravel [QUERY] how to select contents from table 1 where condition must meet in table 2 and table 3Laravel [QUERY] 如何从表 1 中选择必须满足表 2 和表 3 条件的内容
【发布时间】:2017-09-19 18:42:09
【问题描述】:

如果表 2 和表 3 满足条件,我需要从表 1 中选择项目。

我有这三个表:

company -> company_id, name, location

services -> company_id, service_id, service_name

date -> service_id, service_date //should i include company_id here?

如果搜索匹配 services->service_name 和 date->service_date,我想选择 company -> name and location

如何加入表以获得所需的结果?

【问题讨论】:

  • 你已经尝试过什么?请查看How do I ask a good question。您的问题应该包括您的特定编码相关问题的清晰概述、您已经尝试过的内容的摘要以及Minimal, Complete, and Verifiable example 中的相关代码,因此我们有足够的信息可以提供帮助.
  • 您不应该仅仅为了存储时间戳而创建一个表(即去掉date 表)。使service_date 字段成为services 表的一部分,并将@​​987654329@ 上的companyservices 与指定日期范围和/或service_name 的WHERE 子句连接起来。
  • @chb 但是一项服务可以有多个日期,并且也有关联的时间,所以我认为需要标准化。

标签: php mysql sql laravel eloquent


【解决方案1】:

这未经测试,但它会类似于以下内容:

$query = Company::query()
  ->select(['company.name','company.location'])
  ->leftJoin('services', 'services.company_id', '=', 'company.company_id')
  ->leftJoin('date', 'date.service_id', '=', 'services.service_id')
  ->where('services.service_name', $servicename)
  ->where('date.service_date', $servicedate);

【讨论】:

  • 谢谢你,不得不做一些补充,但谢谢你,你是唯一回答我的人,这对我帮助很大:)
猜你喜欢
  • 2012-10-27
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2012-10-27
  • 2015-11-03
相关资源
最近更新 更多