Reputation: 15
please help mee, can you tell me way ?
$totalah = Sah::find()
->select('mhs, SUM(IF(status_kehadiran = 1,status_kehadiran , 0))AS K3211335')
->where([
'kode_mk'=> 'K3211335'
])
->groupBy('mhs')
->all();
echo "<pre/>"; print_r($totalah);
die;
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS
K3211335
FROMsah
WHEREkode_mk
='K3211335' GROUP BYmhs' at line 1 The SQL being executed was: SELECT
mhs, SUM(IF(status_kehadiran = 1,
status_kehadiran,
0))ASAS
K3211335FROM
simak_absen_harianWHERE
kode_mk='K3211335' GROUP BY
mhs`
Upvotes: 1
Views: 241
Reputation: 577
If I undestand you purpose, you sum status_kehadiran
when status_kehadiran = 1
, why you don't just SUM status_kehadiran where status_kehadiran = 1
$totalah = Sah::find()
->select(['mhs', 'SUM(status_kehadiran) AS K3211335'])
->where([
'kode_mk'=> 'K3211335',
'status_kehadiran' => 1
])
->groupBy('mhs')
->all();
Upvotes: 0
Reputation: 1072
For multiple data in select you can use array. You also need a space before AS:
$totalah = Sah::find()
->select(['mhs', 'SUM(IF(status_kehadiran = 1,status_kehadiran , 0)) AS K3211335'])
->where([
'kode_mk'=> 'K3211335'
])
->groupBy('mhs')
->all();
Upvotes: 1