Mysql Laravel联接表和查询

Mysql Laravel联接表和查询,mysql,laravel-5,eloquent,Mysql,Laravel 5,Eloquent,我有三张桌子。用户、帐户和预订。我尝试了三个表联接和年、月查询。[3附表][1] 但查询预订计算显示错误。它显示出双倍的数量 $january = DB::table('users') ->join('bookings', 'users.id', '=', 'bookings.user_id') ->join('accounts', 'users.id', '=', 'accounts.user_id') ->orderBy('u

我有三张桌子。用户、帐户和预订。我尝试了三个表联接和年、月查询。[3附表][1]

但查询预订计算显示错误。它显示出双倍的数量

$january = DB::table('users')
        ->join('bookings', 'users.id', '=', 'bookings.user_id')
        ->join('accounts', 'users.id', '=', 'accounts.user_id')
        ->orderBy('users.room_id','asc')
        ->groupBy('users.id')
        ->whereYear('bookings.bookingdate','=', '2016')
        ->whereMonth('bookings.bookingdate','=','01')
        ->whereYear('accounts.accountdate','=', '2016')
        ->whereMonth('accounts.accountdate','=','01')
        ->select('users.*',
            DB::raw("COUNT(case when bookings.breakfast='on' then 1 else null end) AS t_breakfast"),
            DB::raw("COUNT(case when bookings.lunch='on' then 1 else null end) AS t_lunch"),
            DB::raw("COUNT(case when bookings.dinner='on' then 1 else null end) AS t_dinner"),
            DB::raw("SUM(accounts.amount) AS t_amount")
        )
        ->get();

    dd($january);
我的账户表是: 我的预订表是:

运行此查询时,将显示:

+"t_breakfast": "2"
+"t_lunch": "2"
+"t_dinner": "2"
+"t_amount": "22.00"

但我需要早餐、午餐、晚餐的数量是:1。但它显示出双重性

当我尝试一个查询时,我没有解决这个问题。然后我尝试了两个查询并解决了它

$data = DB::select("SELECT users.id, users.name , users.picture, users.room_id,
        SUM(CASE WHEN bookings.breakfast='on' THEN 1 ELSE 0 END) AS t_b ,
        SUM(CASE WHEN bookings.lunch='on' THEN 1 ELSE 0 END) AS t_l ,
        SUM(CASE WHEN bookings.dinner='on' THEN 1 ELSE 0 END) AS t_d FROM `users`
    INNER JOIN
    `bookings` ON users.id=bookings.user_id AND
    MONTH(bookings.bookingdate) = $month AND
    YEAR(bookings.bookingdate) = $year

    GROUP BY users.id
    ORDER BY users.room_id ASC");

    $datas = [];
    $data = json_decode(json_encode($data),true);
    foreach ($data as $key => $value) {
        $x = [];
        $x = $value;
        $x['exp'] = Account::where('user_id',$x['id'])->whereMonth('accountdate','=',$month)->whereYear('accountdate','=',$year)->sum('amount');
        $datas[] = $x;
    }

    $total_t_b = $total_t_l = $total_t_d = $total_exp = 0;
    foreach ($datas as $data) {
        $total_t_b += $data['t_b'];
        $total_t_l += $data['t_l'];
        $total_t_d += $data['t_d'];
        $total_exp += $data['exp'];

    }
    $g_total = $total_t_b + $total_t_l + $total_t_d;

    if($g_total <= 0) {
        $perbookingprice = 0;
    } else {
        $perbookingprice = $total_exp / $g_total;
    }
$data=DB::select(“选择users.id、users.name、users.picture、users.room\u id,
金额(如预订早餐时为1,否则为0),
金额(如预订时为1,午餐时间为1,否则为0结束)为t_l,
从“用户”处获得的金额(在预订晚餐时为1,否则为0)`
内连接
`bookings`ON users.id=bookings.user\u id和
月份(bookings.bookingdate)=$MONTH和
年份(bookings.bookingdate)=$YEAR
按用户分组。id
用户订购。房间号(ASC);
$datas=[];
$data=json_decode(json_encode($data),true);
foreach($key=>$value形式的数据){
$x=[];
$x=$value;
$x['exp']=Account::where('user_id',$x['id'])->whereMonth('accountdate','=',$month)->whereYear('accountdate','=',$year)->sum('amount');
$datas[]=$x;
}
$total\u t\u b=$total\u t\u l=$total\u t\u d=$total\u exp=0;
foreach($datas作为$data){
$total___b+=$data['t_b'];
$total___l+=$data['t_l'];
$total___d+=$data['t_d'];
$total_exp+=$data['exp'];
}
$g_total=$total_t_b+$total_t_l+$total_d;

如果($g_total当我尝试一个查询时,我没有解决这个问题。然后我尝试了两个查询并解决了它

$data = DB::select("SELECT users.id, users.name , users.picture, users.room_id,
        SUM(CASE WHEN bookings.breakfast='on' THEN 1 ELSE 0 END) AS t_b ,
        SUM(CASE WHEN bookings.lunch='on' THEN 1 ELSE 0 END) AS t_l ,
        SUM(CASE WHEN bookings.dinner='on' THEN 1 ELSE 0 END) AS t_d FROM `users`
    INNER JOIN
    `bookings` ON users.id=bookings.user_id AND
    MONTH(bookings.bookingdate) = $month AND
    YEAR(bookings.bookingdate) = $year

    GROUP BY users.id
    ORDER BY users.room_id ASC");

    $datas = [];
    $data = json_decode(json_encode($data),true);
    foreach ($data as $key => $value) {
        $x = [];
        $x = $value;
        $x['exp'] = Account::where('user_id',$x['id'])->whereMonth('accountdate','=',$month)->whereYear('accountdate','=',$year)->sum('amount');
        $datas[] = $x;
    }

    $total_t_b = $total_t_l = $total_t_d = $total_exp = 0;
    foreach ($datas as $data) {
        $total_t_b += $data['t_b'];
        $total_t_l += $data['t_l'];
        $total_t_d += $data['t_d'];
        $total_exp += $data['exp'];

    }
    $g_total = $total_t_b + $total_t_l + $total_t_d;

    if($g_total <= 0) {
        $perbookingprice = 0;
    } else {
        $perbookingprice = $total_exp / $g_total;
    }
$data=DB::select(“选择users.id、users.name、users.picture、users.room\u id,
金额(如预订早餐时为1,否则为0),
金额(如预订时为1,午餐时间为1,否则为0结束)为t_l,
从“用户”处获得的金额(在预订晚餐时为1,否则为0)`
内连接
`bookings`ON users.id=bookings.user\u id和
月份(bookings.bookingdate)=$MONTH和
年份(bookings.bookingdate)=$YEAR
按用户分组。id
用户订购。房间号(ASC);
$datas=[];
$data=json_decode(json_encode($data),true);
foreach($key=>$value形式的数据){
$x=[];
$x=$value;
$x['exp']=Account::where('user_id',$x['id'])->whereMonth('accountdate','=',$month)->whereYear('accountdate','=',$year)->sum('amount');
$datas[]=$x;
}
$total\u t\u b=$total\u t\u l=$total\u t\u d=$total\u exp=0;
foreach($datas作为$data){
$total___b+=$data['t_b'];
$total___l+=$data['t_l'];
$total___d+=$data['t_d'];
$total_exp+=$data['exp'];
}
$g_total=$total_t_b+$total_t_l+$total_d;

如果($g_total当你说它显示双倍数量时,你是指t_数量吗?嗨,我正在编辑并在我的帖子中添加更多细节。请看我的帖子。你解决了你的问题吗?当我尝试一个查询时,我没有解决这个问题。然后我尝试了两个查询并解决了它。当你说它显示双倍数量时,你是指t_数量吗?嗨,我是编辑一个。)d在我的帖子中添加更多细节。请查看我的帖子。你解决问题了吗?当我尝试一个查询时,我没有解决这个问题。然后我尝试了两个查询并解决了它。