[MYSQL] - Bu Ayki Kayıtlar Problemim
-
Merhaba muritler
https://www.db-fiddle.com/f/swpzmxe4JmGveAfqvjCE6X/1
linkteki gibi bir veritabanım var
amacım bu hatfaki satışlar
bu ayki satılar bu yılkı satıslar olacak sekılde 3 cıktı almak ıstıyorum
bunlarda sıkıntı yok ama benım amacım bu haftakı ıslemlerde gordugunuz usere 1 gunde kayıt olmadıgı ıcın gelmedı ben o gunde gelsın ıstıyorum
sql kayıt yoksa bıle o satır 0 olarak gelsın
-
sql den pek anlamam ama, o select * from tabloadi dedigin yerde, tablo adi degil de baska bir sey vurman lazim bence. salliyorum sql'de select tarih from hafta gibi bir sorgu yazdigin zaman sana 2020-01-01T00:00:00 diye bi tarih doner, iste bunan 1'den 7'ye kadar. daha sonra subquery ile gun gun ilgili tablondan datayi cekip islemlerini yapip listelersin onun haricinde olmayan bir satiri sql'de nasil gostercen?
-
çok bilmemekle berabel Left/Righ/Full Join kullansan ?
-
Full join yaparsan gelir diye biliyorum
-
ben şöyle bir çözüm yaptım
CREATE TEMPORARY TABLE zero(gun INT); insert into zero values (0),(1),(2),(3),(4),(5),(6); select a.gun,ifnull(Tutar,0) as Tutar from (select * from zero) as a left join (select sum(Tutar) as Tutar ,date_format(tarih,'%w') as Gun from Test where tarih >= NOW() - INTERVAL 1 MONTH group by Gun) as b on a.gun=b.gun order by Gun; DROP TEMPORARY TABLE zero;
yada yukarıdaki topa girmeden php kullandığını varsaydım şunu yapsan olmaz mı?
<?php $gun_tutar=[0,0,0,0,0,0,0] foreach ($data as $row) { $gun_tutar[ $row['Gun'] ]+=$row['Tutar'] } foreach($gun_tutar as $Gun => $Tutar) { //bişeyler bişeyler } ?>
-
Kısıtlı SQL bilgim ile şöyle bir hale soktum senin sorguyu, istediğin gibi çalışıyor:
https://www.db-fiddle.com/f/swpzmxe4JmGveAfqvjCE6X/2
-
cvplar ıcın hepınıze tesekkur ederım
gecıcı tablo kullanarak bunu yapabılıyorum zaten ama sıkıntı 30 gun oldugu zamn tabloya 30 satır ekle sonra temızle felam pek duzenlı bır kod gıbı gelmedı o yuzden baska bır yontem arayısına gırdım
-
şöyle birşey yaptım
as a dediğim kısmı internette buldum nasıl çalıştığını irdelemedim (link aşağıda)
artık group by felan sana kalmış
select a.selected_date as Tarih, ifnull(b.Tutar,0) as Tutar from (select * from (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v where selected_date between date_add(now(), interval -30 DAY ) and now() ) as a left join (select Tutar,Tarih from Test where tarih >= NOW() - INTERVAL 1 MONTH ) as b on a.selected_date=date_format(b.Tarih,'%Y-%m-%d')
-
wert bunu yazdı
şöyle birşey yaptım
as a dediğim kısmı internette buldum nasıl çalıştığını irdelemedim (link aşağıda)
artık group by felan sana kalmış
select a.selected_date as Tarih, ifnull(b.Tutar,0) as Tutar from (select * from (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v where selected_date between date_add(now(), interval -30 DAY ) and now() ) as a left join (select Tutar,Tarih from Test where tarih >= NOW() - INTERVAL 1 MONTH ) as b on a.selected_date=date_format(b.Tarih,'%Y-%m-%d')
tşk ederim hocam bunuda gordum
burda sıkıntı bu hafta degılde bu ay dedıgımde
30 tane satır yazmam gerekır
en mantıklı yol olan kayıtları secıp php de esleme yapmak galıba
-
https://www.db-fiddle.com/f/oJ2SpDmS9ut9crFmJFzCQa/0
duz mantik generic bir sey yazdim table yaratmadan. tum gunleri listeliyor ama @start i parametre ile alip filtrelemek istedigin tarihler arasini versen calisir buyuk ihtimalle.
edit: procedure olmasinin sebebi limit e dynamic parametre verilmiyor. ama kod icinde dynamic olusturabilirsin onu da.
musdawdaf tarafından 24/Haz/20 11:30 tarihinde düzenlenmiştir -
musdawdaf bunu yazdı
https://www.db-fiddle.com/f/oJ2SpDmS9ut9crFmJFzCQa/0
duz mantik generic bir sey yazdim table yaratmadan. tum gunleri listeliyor ama @start i parametre ile alip filtrelemek istedigin tarihler arasini versen calisir buyuk ihtimalle.
edit: procedure olmasinin sebebi limit e dynamic parametre verilmiyor. ama kod icinde dynamic olusturabilirsin onu da.
tşk ederim hocam
dedıgım gıbı bu hafta - bu ay ve bu yıl olacak sekılde uc varyasyonum vardı bende bu yuzden asagıdakı sekılde phpde hallettım
$tempVeri = $rpVT ->from('tb_satis') ->select("date_format(tarih,'%a') as Sure,SUM(Tutar) as Tutar") ->where('(MONTH(Tarih)=MONTH(NOW()) AND YEAR(Tarih)=YEAR(NOW()) AND WEEK(Tarih)=WEEK(NOW()))','','SQL') ->where('BayiKodu','1') ->groupby('Sure') ->all(); $KazancGunler=['Mon'=>0,'Tue'=>0,'Wed'=>0,'Thu'=>0,'Fri'=>0,'Sat'=>0,'Sun'=>0]; foreach ($tempVeri as $value) { $KazancGunler[$value['Sure']]=$value['Tutar']; $Toplam+=$value['Tutar']; } foreach ($KazancGunler as $value) { $toArray[]=$value; } $Veri['Labels']=['Pazartesi','Salı','Çarşamba','Perşembe','Cuma','Cumartesi','Pazar']; $Veri['Datas']=$toArray; $Veri['Toplam']= number_format($Toplam, 2);