【问题标题】:How to select common date from three tables in laravel?如何从 laravel 的三个表中选择公共日期?
【发布时间】:2020-02-03 09:46:23
【问题描述】:

我的数据库中有这三个表,我想从三个表中选择共同的日期来计算每天的利润,每个月的利润,每年的利润

这是我的桌子:

Schema::create('expenses', function (Blueprint $table) {
    $table->bigIncrements('id');            
    $table->string('name');
    $table->float('amount');
    $table->string('month');
    $table->string('year');
    $table->string('date');
});

Schema::create('service_orders', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('client_id')->unsigned();
    $table->double('total_price', 8, 2)->nullable();
    $table->string('month');
    $table->string('year');
    $table->date('date');
    $table
        ->foreign('client_id')
        ->references('id')
        ->on('clients')
        ->onDelete('cascade');
});
Schema::create('orders', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('client_id')->unsigned();
    $table->double('total_price', 8, 2)->nullable();
    $table->string('month');
    $table->string('year');
    $table->date('date');
    $table
        ->foreign('client_id')
        ->references('id')
        ->on('clients')
        ->onDelete('cascade');
});

【问题讨论】:

  • 尝试使用join 查询 - laravel.com/docs/5.8/queries
  • 我会建议你,根据你想要的日期获取 3 种不同的数据。之后,通过php计算。这对你来说会更容易。

标签: eloquent laravel-6.2


【解决方案1】:

我认为你的问题可以通过 UNIONS 解决

UNION 用于合并两个或多个表的结果

  SELECT date FROM expenses
  UNION ALL
  SELECT date FROM service_orders
  UNION ALL
  SELECT date FROM orders

要在 Laravel 中使用它,您可以查看以下文档:

https://laravel.com/docs/5.8/queries#unions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多