【问题标题】:Join two tables with all records连接两个包含所有记录的表
【发布时间】:2020-12-04 12:18:41
【问题描述】:

我正在尝试在我的 Laravel 控制器代码中加入两个表,并在一个 Datatable 中查看它们。

table1

+--------------------+---------+
|     recordtime     | tempout |
+--------------------+---------+
| 4.12.2020 10:00:00 |     1.1 |
| 4.12.2020 10:30:00 |     1.2 |
| 4.12.2020 11:00:00 |     1.3 |
| 4.12.2020 11:30:00 |     1.4 |
| 4.12.2020 12:00:00 |     1.5 |
+--------------------+---------+

table2

+--------------------+---------+
|     recordtime     | tempout |
+--------------------+---------+
| 4.12.2020 10:00:00 |     2.1 |
| 4.12.2020 11:00:00 |     2.3 |
| 4.12.2020 12:00:00 |     2.5 |
| 4.12.2020 13:00:00 |     2.6 |
| 4.12.2020 14:00:00 |     2.7 |
+--------------------+---------+

当我使用这段代码时:

$results  = Tablemodel1::whereBetween('table1.recordtime', $dateScope)
    ->selectRaw('table1.tempout,table2.tempout as tempoutstamb,table2.recordtime')
    ->leftJoin('table2', function($join){
        $join->on('table1.recordtime', '=', 'table2.recordtime');
    })
    ->orderBy('table1.recordtime', 'ASC')
    ->get();

return Datatables::of($results)
    ->make(true);

它给了我所有等于相应记录时间的记录,以及每半小时一次的附加记录,但具有来自table1 的空(无效日期)值。如何显示他们的日期而不是 null(无效日期)?

+--------------------+---------+--------------+
|     recordtime     | tempout | tempoutstamb |
+--------------------+---------+--------------+
| invalid date       |     1.2 |            - |
| invalid date       |     1.4 |            - |
| 4.12.2020 10:00:00 |     2.1 |          1.1 |
| 4.12.2020 11:00:00 |     2.3 |          1.3 |
| 4.12.2020 12:00:00 |     2.5 |          1.5 |
+--------------------+---------+--------------+

添加了基于@miken32 答案的工作 Laravel 查询:

  $results2  = Tablemodel1::whereBetween('table1.recordtime', $dateScope)
    ->selectRaw('table1.recordtime')
    ->selectRaw('max(table1.tempout) as tempout')
    ->selectRaw('max(table2.tempout) as tempoutstamb')        
    ->leftJoin('table2', function($join){
        $join->on('table1.recordtime', '=', 'table2.recordtime');
    })
    ->groupBy('table1.recordtime');
  $results = Tablemodel2::whereBetween('table2.recordtime', $dateScope)
    ->selectRaw('table2.recordtime')
    ->selectRaw('max(table1.tempout) as tempout')
    ->selectRaw('max(table2.tempout) as tempoutstamb')        
    ->leftJoin('table1', function($join){
        $join->on('table1.recordtime', '=', 'table2.recordtime');
    })
    ->groupBy('table2.recordtime')       
    ->orderBy('recordtime', 'ASC')
    ->union($students2)
    ->get();

【问题讨论】:

  • 它为您提供无效的日期或表 2 的空数据,因为您使用了左连接
  • 好的,使用什么连接来给我所有数据?因为如果我只使用Join,那么只有两个表中的数据都符合日期时间,但没有其他数据。
  • 请考虑阅读此主题:stackoverflow.com/questions/5706437/… 对 sql 中的联接有深入的了解,将为您提供更好的未来!
  • 我看到在 tempoutstamb 中看到空数据是正常的,但是在这个线程中,leftjoin 似乎也需要显示该记录时间的实际值?
  • @HristianYordanov 如果您想要真实/表格所有数据,那么我们可以在 table2 的选择中使用“coalesce”或“if”,这样当 table2 id 为空时,我们可以放置所需的数据,例如没有日期可用或“00-00-00 00:00:00”

标签: php mysql laravel left-join


【解决方案1】:

这是does the trick的一些SQL:

SELECT table1.recordtime, table1.tempout, table2.tempout AS tempoutstamb
FROM table1
LEFT JOIN table2 ON (table1.recordtime = table2.recordtime)

UNION

SELECT table2.recordtime, table1.tempout, table2.tempout AS tempoutstamb
FROM table2
LEFT JOIN table1 ON (table1.recordtime = table2.recordtime)

ORDER BY recordtime

您正在寻找完全联接,但 MySQL doesn't do those。所以我们用UNION 查询来伪造它。

为了在 Laravel 中使用,可能最简单的方法是将整个东西包装起来 in a raw statement

【讨论】:

  • 感谢您的回答。我通过添加有效的 Laravel 查询来编辑我的帖子。我想知道我们可以添加一张具有相同结构和方式的表格吗?因为我正在尝试一段时间来做到这一点。但是是一团糟并添加了更多的代码重复。
  • 如果这回答了您的问题,请将其标记为已接受,然后发布一个新问题。但总的来说,您没有理由不能向此查询添加多个表。
  • 您能否检查与此相关的新问题,请stackoverflow.com/questions/65236765/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多