Rendra
Rendra

Reputation: 15

Database Exception in yii2

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 FROM sah WHERE kode_mk='K3211335' GROUP BY mhs' at line 1 The SQL being executed was: SELECT mhs, SUM(IF(status_kehadiran = 1, status_kehadiran, 0))ASASK3211335FROMsimak_absen_harianWHEREkode_mk='K3211335' GROUP BY mhs`

Upvotes: 1

Views: 241

Answers (2)

vvpanchev
vvpanchev

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

Md. Salahuddin
Md. Salahuddin

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

Related Questions