0

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

这是我的桌子:

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');
});
4

1 回答 1

0

我认为您的问题可以通过 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

于 2020-02-03T11:15:48.520 回答