PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE results ( id int, opponent_id int, result enum('won','lost'), points int ); INSERT INTO results (id, opponent_id, result, points) VALUES (1, 10, 'won', 2), (1, 11, 'lost', 0), (1, 12, 'lost', 0), (1, 10, 'won', 2), (1, 11, 'lost', 0), (1, 10, 'lost', 0), (1, 12, 'won', 2);
Copy Clear
Copy Format Clear
<?php var_export( $db::table('results') ->select('opponent_id') ->selectRaw("SUM(result = 'won') won") ->selectRaw("SUM(result = 'lost') lost") ->selectRaw("SUM(points) points") ->where(['id' => 1]) ->groupBy('opponent_id') ->get() ->toArray() );
Show:  
Copy Clear