具有连接表的多列SUM - Laravel 5.2

时间:2017-11-06 05:47:29

标签: php laravel

我有两张表winnerscheck_winners

赢家:

id     check_winner_id      username     win_number ....
 1                   1           xxx              1
 2                   2           xxx              1
 3                   3           yyy              1
 4                   4           yyy              1

check_winners:

id       user_id      chance_of_win....
 1             1                  1
 2             1                  1
 3             2                  1
 4             2                  1
 5             2                  1
 6             2                  1
 7             2                  1
 8             2                  1

现在我想要join两个表,并希望将chance_of_win列和win_number列组加username。我尝试了一种方法,但它没有给出win_number之和的确切结果。我怎样才能做到这一点?

$winners = DB::table('winners')
            ->groupBy('winners.username')
            ->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
            ->selectRaw('*, sum(check_winners.chance_of_win) as chance_sum, sum(winners.win_number) as win_sum')         
dd($winners);

用这个我得到输出:

array:2 [▼
  0 => {#241 ▼
    +"id": 1
    +"check_winner_id": 1
    +"username": "xxx"
    +"chance_sum": "2"
    +"win_sum": "2"
  }
  1 => {#242 ▼
    +"id": 2
    +"check_winner_id": 2
    +"username": "yyy"
    +"chance_sum": "6"
    +"win_sum": "6"
  }
]

win_sum 2 array <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/SMTP.php'; $mail = new PHPMailer(true); //Obtener datos try { $name = $_POST['name']; $lastName = $_POST['lastName']; $email = $_POST['email']; $speciality = $_POST['speciality']; $title = $_POST['title']; $phone = $_POST['phone']; $adress = $_POST['adress']; $phone = $_POST['phone']; $cellphone = $_POST['cellphone']; $state = $_POST['state']; $colony = $_POST['colony']; $rfc = $_POST['rfc']; //Luego tenemos que iniciar la validación por SMTP: // $mail->IsSMTP(); $mail->SMTPDebug = 2; $mail->SMTPAuth = true; $mail->SMTPSecure = 'tls'; $mail->Host = "krishna.hosting-mexico.net"; $mail->Username = "registros@aliar.svri.org.mx"; . $mail->Password = "xxxx"; $mail->Port = 465; $mail->From = "registros@aliar.svri.org.mx"; $mail->FromName = "Registros Aliar"; $mail->AddAddress("mauricioandres288@gmail.com"); //Content // $mail->isHTML(true); // Set email format to HTML // $mail->Subject = 'Here is the subject'; // $mail->Body = 'This is the HTML message body <b>in bold!</b>'; // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // $mail->send(); $mail->IsHTML(true); // El correo se envía como HTML $mail->Subject = "Nuevo registro de" . $name . " " . $lastName; // Este es el titulo del email. $mail->Body = "Nuevo registro de" . $name . " " . $lastName; $mail->Body .= "<br><h4>Datos personales: </h4>"; $mail->Body .= "<ul><li>" . $name . "</li>"; $mail->Body .= "<li>" . $lastName . "</li>"; $mail->Body .= "<li>" . $email . "</li>"; $mail->Body .= "<li>" . $speciality . "</li>"; $mail->Body .= "<li>" . $title . "</li>"; $mail->Body .= "<li>" . $phone . "</li>"; $mail->Body .= "<li>" . $adress . "</li>"; $mail->Body .= "<li>" . $phone . "</li>"; $mail->Body .= "<li>" . $cellphone . "</li>"; $mail->Body .= "<li>" . $state . "</li>"; $mail->Body .= "<li>" . $colony . "</li>"; $mail->Body .= "<li>" . $rfc . "</li></ul>"; // $mail->Body = $body; $mail->send(); } catch (Exception $e) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } ?> 。我哪里做错了?提前谢谢。

1 个答案:

答案 0 :(得分:0)

你应该试试这个:

$winners = DB::table('winners')
            ->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
            ->selectRaw('*, sum(check_winners.chance_of_win) as chance_sum, sum(winners.win_number) as win_sum')
            ->groupBy('winners.username')       
dd($winners);

更新回答

$winners = DB::table('winners')
            ->select('*', DB::raw("SUM(check_winners.chance_of_win) as chance_sum"), DB::raw("SUM(winners.win_number) as win_sum"))
            ->leftJoin('check_winners', 'winners.id', '=', 'check_winners.user_id')
            ->groupBy('winners.username')