• 01-08-2007, 20:17:42
    #1
    Yapmak istediğim eğer veri yoksa oluşturmak, eğer varsa onu güncellemek.
    Ama hata veriyor nasıl olacak? Bir fikri olan var mı?

    SQL:
     if exists (select kisi from sayac_rapor where webid='toplam_sayac' and tarih='2007-05-07' limit 1)
    update sayac_rapor set kisi=(kisi+1) where webid='toplam_sayac' and tarih='2007-05-07' limit 1;
    else
    insert into sayac_rapor values(NULL,'toplam_sayac','2007-05-07','1');
    phpMyAdmin'de test ettiğimdeki hata:

    SQL sorgusu:     
                  IF  EXISTS ( SELECT kisi
    FROM sayac_rapor
    WHERE webid =  'toplam_sayac'
    AND tarih =  '2007-05-07'
    LIMIT 1 
    )
    UPDATE sayac_rapor SET kisi = ( kisi +1  )  WHERE webid =  'toplam_sayac' AND tarih =  '2007-05-07' LIMIT 1 ;
    
         
    MySQL çıktısı:  
      #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 'if exists (select kisi from sayac_rapor where webid='toplam_sayac' and tarih='20' at line 1
    Saygılar.
  • 04-08-2007, 01:17:40
    #2
    şu şekil yapabilirsin asnırım.
    <?
    /*
    ...
    veritabanı ayarları
    ...
    */
    
    $sorgu=@mysql_query("SELECT kisi FROM sayac_rapor WHERE webid =  'toplam_sayac' AND tarih =  '2007-05-07'");
    $sayi=mysql_numrows($sorgu);
    
    if($sayi>0)
    mysql_query("update ........");
    else
    mysql_query("insert ......");
    ?>
    </span>
  • 04-08-2007, 15:45:03
    #3
    Üyeliği durduruldu
     if exists (select kisi from sayac_rapor where webid='toplam_sayac' and tarih='2007-05-07' limit 1) begin
    update sayac_rapor set kisi=(kisi+1) where webid='toplam_sayac' and tarih='2007-05-07' limit 1; end
    else begin
    insert into sayac_rapor values(NULL,'toplam_sayac','2007-05-07','1');
    end
    Dene bir, yada then endif, fln denersin.
  • 05-08-2007, 16:41:55
    #4
    Arkadaşlar teşekkür ederim cevaplar için.

    Seyranli dediğin doğru ama en kısa nasıl yaparım onu öğrenmek istiyorum.
    Lisans dediğini denedim olmadı sonra şunu denedim yine olmadı

    if (exists (select kisi from sayac_rapor where webid='toplam_sayac' and tarih='2007-05-07' limit 1  ))
     begin
      update sayac_rapor set kisi=(kisi+1) where webid='toplam_sayac' and tarih='2007-05-07' limit 1;
     end
    else
     begin
      insert into sayac_rapor values(NULL,'toplam_sayac','2007-05-07','1');
     end
    Hatta iyice basitleştiriyim deneme amaçlı dedim

    if ((select count(*) from sayac_rapor where tarih = '2007-06-12') > 0)
      Print 'Record exits'
    ELSE
      Print 'Record doesn''t exist'
    Gibi birşey yazdım yine hata yine hata
    Php my adminde aldığım hata:

    if( (
     SELECT count(  *  ) 
    FROM sayac_rapor
    WHERE tarih =  '2007-06-12'  )  >0
    )Print 'Record exits'
    ELSE Print 'Record doesn''t exist'
    LIMIT 0 , 30           MySQL çıktısı:  
      #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 'if( ( select count( * ) from sayac_rapor where tarih = '2007-06-12' ) > 0 )  Pri' at line 1
    Hata nerde çözemedim. O kadar araştırdım yok
  • 05-08-2007, 21:09:00
    #5
    Üyeliği durduruldu
    mssql'de
    if(şart) begin
    ...
    end
    blokları arasında yapılıyor ama mysql'de begin end yerine then endif diye biliyorum tabi hiç denemedim o ayrı konu mysql'in sitesinden bulduğum bir kodu denemiştim ondada olmamıştı S:
    sen yinede bir
    if(şart) then
    ..
    endif
    else then
    ..
    end
    dene bakalım.
  • 05-08-2007, 21:14:13
    #6
    yok orası
    if ($şart) then
    // işlemler
    else
    // diğer işlemler
    end if;

    şeklinde olması gerekiyor. ama ben yine de beceremedim o olayı.
    select count(*) into sayi from tablo diyorum, sayi declare edilmemiş diyor,
    declare et diyorum begin end yok diyor, begin end koyuyorum declare konutunu tanımaz oluyor :S