[SQL] Abfrage 2 Tabellen + AVG() + COUNT()

Dieses Thema im Forum "Webentwicklung" wurde erstellt von Aeonsam, 9. Februar 2009 .

Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 9. Februar 2009
    Abfrage 2 Tabellen + AVG() + COUNT()

    Hallo Leute ich komm einfach nicht weiter:


    so leufts
    PHP:
    select t1 . pic_id t1 . activ t2 . pic_id t2 . vote t2 . geschlecht 
    FROM pics 
    AS  t1 pic_vote  AS  t2 
    where t1
    . pic_id  t2 . pic_id  and  t2 . geschlecht = 'w'  and  t1 . activ  = '1'  LIMIT 10 ;
    so nicht
    PHP:
    select t1 . pic_id t1 . activ t2 . pic_id t2 . AVG ( vote ),  t2 . geschlecht 
    FROM pics 
    AS  t1 pic_vote  AS  t2 
    where t1
    . pic_id  t2 . pic_id  and  t2 . geschlecht = 'w'  and  t1 . activ  = '1'  LIMIT 10 ;
    FEHLER AUSGABE SQL
    Code:
    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 '(AVG(vote)), t2.geschlecht FROM pics AS t1, pic_vote AS t2 where t1.pic_id = t2.' at line 1
    eine normale abfrge ist kein thema:
    select AVG(vote) from pic_vote;


    hat jemand eine Idee
     
  2. 10. Februar 2009
    AW: Abfrage 2 Tabellen + (AVG)

    falsch: t2.AVG(vote)
    richtig: AVG(t2.vote)

    erledigt
     
  3. 10. Februar 2009
    AW: Abfrage 2 Tabellen + (AVG)

    Hi, danke erstmal für die Hilfe.

    Hat supi geklappt. Blöd das hätte ich sehen müssen.
    BIN jetzt vor einem weiteren fehler


    so lese ich in der regel den datensatz bei mir aus:
    Code:
    $top_pic_schitt = mysql_result($res_top, $i, "AVG(t2.vote)");

    das klappt hier leider nicht.

    FEHLER
    Code:
    mysql_result() [function.mysql-result]: AVG(t2.vote) not found in MySQL result index

    ales andere leuft er sortiert richtig und in SQL abfrage direct gibt er mir auch den AVG(t2.vote) aus.
     
  4. 10. Februar 2009
    AW: Abfrage 2 Tabellen + (AVG)

    Ich denke es liegt daran, dass du eine Aggregatfunktion verwendest. Versuche die Ergebnisspalte mal mittels Alias anders zu bennen und das Alias fragst du dann ab.

    PHP:
    select t1 . pic_id t1 . activ t2 . pic_id AVG ( t2 . vote ) AS  avg_t2 t2 . geschlecht 

    und dann halt

    PHP:
    $top_pic_schitt  =          mysql_result ( $res_top $i "avg_t2" );
     
  5. 10. Februar 2009
    AW: Abfrage 2 Tabellen + (AVG)

    mysql_result unterstützt diese drei Dinge
    Der Quote ist etwas verwirrend, kann dir aber direkt ein Tip für dein Problem geben. Entweder einen Alias setzen oder den numerischen Offset nutzen.
     
  6. 10. Februar 2009
    AW: Abfrage 2 Tabellen + (AVG)

    so hats super funktioniert. Danke Leute.
    bw habt ihr




    *closed*
     
  7. 10. Februar 2009
    AW: Abfrage 2 Tabellen + (AVG)

    oder auch nicht closed, damn stehe vor der nechsten wand.

    WILL NUN NUR DIE AUSGABE MIT MIDESTENS 10 COUNTS
    den count lese ich ohne probleme aus mit COUNT(t2.vote) AS count_t2
    nun muss ich das ganze irgentwie in die where abfrage reinbringen und da scheitere ich leider.


    so leufts
    PHP:
    [ HTML ] select t1 . pic_id t1 . activ t2 . pic_id AVG ( t2 . vote ) AS  avg_t2 COUNT ( t2 . vote ) AS  count_t2 t2 . geschlecht 
                                                       FROM pics 
    AS  t1 pic_vote  AS  t2 where 
                                                       t1
    . pic_id  t2 . pic_id  and  t2 . geschlecht = 'm'  and  t1 . activ  = '1'
                                                       
    group by t2 . pic_id order by AVG ( t2 . vote DESC LIMIT 10 ; ");
    so nicht

    PHP:
    select t1 . pic_id t1 . activ t2 . pic_id AVG ( t2 . vote ) AS  avg_t2 COUNT ( t2 . vote ) AS  count_t2 t2 . geschlecht 
                                                       FROM pics 
    AS  t1 pic_vote  AS  t2 where 
                                                       t1
    . pic_id  t2 . pic_id  and  t2 . geschlecht = 'm'  and  t1 . activ  = '1' 
                                                       
    and  COUNT ( t2 . vote ) >=  10
                                                       group by t2
    . pic_id order by AVG ( t2 . vote DESC LIMIT 10 ; ");
    was nun? das ist bestimmt voll billig nur ich hänge da fest
     
  8. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.