martins256 Posted October 3, 2007 Report Share Posted October 3, 2007 Sveiki! Cik nu spēju izmeklēju pa googli, bet tāpat neko daudz nesapratu, tāpēc lūdzu palīdzību te. Tātad man ir 2 tabulas t1 sastāv no `id`(auto incr., primary key) un `kaaposti` t2 sastāv no `user_id` un `kaaposti_h` Vēlamais rezultāts būtu ar 1 query panākt, lai t2 tiek sasummēti (attiecīgi katram user_id) `kaaposti_h`, un tad iegūtā summa pieskaitīta `kaaposti` iekš t2, kur t2.id = t2.user id Link to comment Share on other sites More sharing options...
andrisp Posted October 3, 2007 Report Share Posted October 3, 2007 Man liekas, ka ar vienu kveriju to nevarēs panākt. Link to comment Share on other sites More sharing options...
Grey_Wolf Posted October 3, 2007 Report Share Posted October 3, 2007 andrisp --> var tikai taads stipri samociits sanaaks ... (ne parak sarezgjits) martins256 -> skaties pec SUM() .... tajaa SUM( ) var sabakstiit ieksa ljoti daudz..... + noteikti naksies izmantot ari AS ..... ---- Slinkums tagat rakstiit ... --- P.S. izmanto MYSQL manuali .... (vinsh gan taads ljoti, ljoti neparskatams , bet izburties var) // MYSQ.COM .... Link to comment Share on other sites More sharing options...
Gints Plivna Posted October 3, 2007 Report Share Posted October 3, 2007 Soory par nedaudz offtopiku (bāzes sakarā) bet oraclē tas būtu šādi: Vispirms uztaisam tabulas un iebāžam kaut kādus datus: SQL> create table t1(id number primary key, kaaposti number not null); Table created. SQL> ed Wrote file afiedt.buf 1* create table t2(user_id number not null, kaaposti_h number not null) SQL> / Table created. SQL> ed Wrote file afiedt.buf 1* insert into t1 select rownum, rownum from dba_objects where rownum <=10 SQL> / 10 rows created. SQL> commit; Commit complete. SQL> select * from t1; ID KAAPOSTI ---------- ---------- 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 10 rows selected. SQL> insert into t2 select mod(rownum, 10) + 1, rownum from dba_objects where rownum <=100; 100 rows created. SQL> select * from t2 where rownum <=12; USER_ID KAAPOSTI_H ---------- ---------- 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 1 10 2 11 3 12 12 rows selected. Tagad sākam pa klucītim būvēt to ko mums vajag, vispirms sasummējam t2 ierakstus: SQL> select user_id, sum(kaaposti_h) 2 from t2 3 group by user_id; USER_ID SUM(KAAPOSTI_H) ---------- --------------- 1 550 6 500 2 460 4 480 5 490 8 520 3 470 7 510 9 530 10 540 10 rows selected. Vismaz oraclē šādu te datu kopu nekas nekavē izmantot tālāk un joinot ar citu tabulu: SQL> select * from ( 2 select user_id, sum(kaaposti_h) 3 from t2 4 group by user_id) summary, t1 5 where t1.id = summary.user_id; USER_ID SUM(KAAPOSTI_H) ID KAAPOSTI ---------- --------------- ---------- ---------- 3 470 3 3 7 510 7 7 2 460 2 2 8 520 8 8 10 540 10 10 9 530 9 9 4 480 4 4 5 490 5 5 6 500 6 6 1 550 1 1 10 rows selected. Nu un tagad atliek tikai saskaitīt 2 kolonas un tikt vaļā no 2 reiz viena un tā paša ida: SQL> ed Wrote file afiedt.buf 1 select id, kaaposti_h_summa + kaaposti from ( 2 select user_id, sum(kaaposti_h) kaaposti_h_summa 3 from t2 4 group by user_id) summary, t1 5* where t1.id = summary.user_id SQL> / ID KAAPOSTI_H_SUMMA+KAAPOSTI ---------- ------------------------- 3 473 7 517 2 462 8 528 10 550 9 539 4 484 5 495 6 506 1 551 10 rows selected. Tā kā manas zināšanas par MySQL (BTW tas ir MySQL? sākotnējā jautājumā jau nekur tas netika pateikts :) ir visai minorīgas, tad ja nu gadījumā šitais nestrādā, tad jebkurā gadījumā būtu jāstrādā 2 soļos: 1) views uz t2, kas taisa group by 2) joins starp t1 un viewu Gints Plivna http://www.gplivna.eu Link to comment Share on other sites More sharing options...
Paulinjsh Posted October 3, 2007 Report Share Posted October 3, 2007 uz mysql arī vajadzētu strādāt (vismaz uz 5. versijas) Link to comment Share on other sites More sharing options...
Grey_Wolf Posted October 4, 2007 Report Share Posted October 4, 2007 Gints Plivna --> SQL arii Afrikaa ir SQL ;) Uz MySQL kveriijs sanaak llidziigs ... ir gan atskjiriibas sintaksee, bet princips liidziigs... teiksim: 2 select user_id, sum(kaaposti_h) kaaposti_h_summa buutu : select user_id, sum(kaaposti_h) AS kaaposti_h_summa ... --- Bet kaa jau mineju pamatprincips liidziigs.... Paulinjsh --> sadus kverijus (ar vairakiem SELECT, Update utt...) var izmantot sakot no 4.1 versijas... // 4.0 un 4.1 versijas ir Butiskas askjiriibas , pat nesaprotu kapec vinji nenosauca to par vers 5.0 ..... Link to comment Share on other sites More sharing options...
bubu Posted October 4, 2007 Report Share Posted October 4, 2007 Grey_Wolf: AS keywords MySQLā ir opcionāls. Bez tā var mierīgi iztikt. Link to comment Share on other sites More sharing options...
Gints Plivna Posted October 4, 2007 Report Share Posted October 4, 2007 Gints Plivna --> SQL arii Afrikaa ir SQL ;) Njā tikai cik atceros tad 3 versijā piemēram subqueriji MySQLā nebija, bet nu OK, tas jau laikam ir sen noiets etaps :) Gints Plivna http://www.gplivna.eu Link to comment Share on other sites More sharing options...
Grey_Wolf Posted October 4, 2007 Report Share Posted October 4, 2007 (edited) Njā tikai cik atceros tad 3 versijā piemēram subqueriji MySQLā nebija Aga... kaa jau mineju tie paradijas 4.1 ;) bubu --> nebiju pieversis tam uzmaniibu , parasti to lietoju, jo taa ir parskatamak (vieglak lasiit) .... Edited October 4, 2007 by Grey_Wolf Link to comment Share on other sites More sharing options...
martins256 Posted October 4, 2007 Author Report Share Posted October 4, 2007 Paldies visiem it īpaši Gintam! Rezultāts izskatās šādi... UPDATE ( SELECT user_id, sum( kaaposti_h ) AS summa FROM t2 GROUP BY user_id ) summary, t1 SET t1.kaaposti = t1.kaaposti + summary.summa WHERE t1.id = summary.user_id; Link to comment Share on other sites More sharing options...
andrisp Posted October 4, 2007 Report Share Posted October 4, 2007 Šitāds kverijs tiešām strādā ? Link to comment Share on other sites More sharing options...
martins256 Posted October 4, 2007 Author Report Share Posted October 4, 2007 nu tur nav kāposti un t1,t2, bet tiešām strādā! Link to comment Share on other sites More sharing options...
Recommended Posts