Visualizzazione risultati 1 fino 16 di 16

Discussione: Ordinamento con union

  1. #1
    Guest

    Predefinito Ordinamento con union

    Salve, mi servirebbe il vostro aiuto per risolvere un problema.
    Seguendo la guida di uno script php per l'ordinamento dei dati di una query dopo la ricerca.

    La query originale prevedeva l'estrazione dei dati da una tabella sola, tramite un UNION ora la query preleva correttamente i dati da due tabelle.

    Il problema si verifica quando la query fa l'ordinamento.

    Questa è la query (è un pò lunga)
    Codice PHP:
    SELECT ROUND((((tab1.title REGEXP '[[:<:]]cerca[[:>:]]')>0)*5+((tab1.text REGEXP '[[:<:]]cerca[[:>:]]')>0)*3+0)*100/8,2) as rilevanza, tab1.id FROM tab1 WHERE tab1.title REGEXP '[[:<:]]cerca[[:>:]]' OR tab1.text REGEXP '[[:<:]]cerca[[:>:]]' UNION SELECT ROUND((((tab2.title REGEXP '[[:<:]]cerca[[:>:]]')>0)*5+((tab2.text REGEXP '[[:<:]]cerca[[:>:]]')>0)*3+0)*100/8,2) as rilevanza, tab2.id FROM tab2 WHERE tab2.title REGEXP '[[:<:]]cerca[[:>:]]' OR tab2.text REGEXP '[[:<:]]cerca[[:>:]]' OR order by rilevanza DESC
    Potete aiutarmi a risolvere il problema?

    Questo è l'errore:
    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 'order by rilevanza DESC' at line 1

    Grazie

  2. #2
    Guest

    Predefinito

    C'è un Or seza seguito.

    OR order by ...

  3. #3
    Guest

    Predefinito

    Grazie mille non me ne ero accorto.
    Posso farti una domanda?

    Dalla query io estraggo in ordine di rilevanza gli id dalle due tabelle, vorrei poi grazie gli id scaricare tutti gli altri dati.

    Posso ricavare la tabella dalla quale proviene l'id?

  4. #4
    Guest

    Predefinito

    potresti effettuare la select così

    Codice:
    SELECT 'tab1' as tabella, ROUND ...
    FROM tab1
    ...
    UNION SELECT 'tab2' as tabella, ROUND ...
    FROM tab2
    ...

  5. #5
    Guest

    Predefinito

    Cosi facendo posso scaricare la variabile tabella?

  6. #6
    Guest

    Predefinito

    Esatto.
    Per ogni record, oltre ai tuioi dati avrai una tab1 o tab2 (o il valore che imposti tu) a seconda da dove vengono i dati.
    Se provi la query ti rendi conto di cosa ottieni.

  7. #7
    Guest

    Predefinito

    Grazia mille

  8. #8
    Guest

    Predefinito

    Ciao, scusa se ti disturbo... Ho ancora qualche problema, nel senso che la query non funziona.

    Puoi dare uno sguardo alla funziona che estrae i risultati? il problema sta nel st=2

    Codice PHP:
    function CreaQueryRicerca($queryvar, $pesotitolo=5, $pesotesto=3, $searchlevel=1, $st)
    {
    // trasformo la stringa in un array di parole da cercare
    $arrayToFind=QueryToArray($queryvar);
    // numero elementi da cercare
    $elementiToFind=count($arrayToFind);
    // punteggio massimo raggiungibile
    $maxPoint=$elementiToFind*$pesotitolo+$elementiToFind*$pesotesto;

    if(
    $elementiToFind==0)
    {
    return
    "";
    } else {
    $query="SELECT 'portali' as tabella, ROUND((";
    $query2="SELECT 'programmi' as tabella, ROUND((";
    $sqlwhere="";
    $sqlwhere2="";

    // ciclo per ogni parola trovata ($Valore)
    foreach($arrayToFind As $Indice => $Valore)
    {
    // se $Valore è presente in titolo instr(titolo, '$Valore') restituirà 1 altrimenti 0
    // moltiplico il valore restituito (1 o 0) per il peso della parola (5 per il titolo, 3 per testo)
    if($st == "1")
    {
    if(
    $searchlevel==1)
    {
    // regexp: uso espressioni regolari. [[:<:]] equivale a \b per separare parole
    $query.="((title REGEXP '[[:<:]]".$Valore."[[:>:]]')>0)*$pesotitolo+";
    $query.="((text REGEXP '[[:<:]]".$Valore."[[:>:]]')>0)*$pesotesto+";
    $sqlwhere.="title REGEXP '[[:<:]]".$Valore."[[:>:]]' OR text REGEXP '[[:<:]]".$Valore."[[:>:]]' OR ";
    } else {
    $query.="(instr(title, '$Valore')>0)*$pesotitolo+";
    $query.="(instr(text, '$Valore')>0)*$pesotesto+";
    $sqlwhere.="title like '%$Valore%' OR text like '%$Valore%' OR ";
    }
    }
    else if(
    $st == "2")
    {
    if(
    $searchlevel==1)
    {
    // regexp: uso espressioni regolari. [[:<:]] equivale a \b per separare parole
    $query.="((tab1.title REGEXP '[[:<:]]".$Valore."[[:>:]]')>0)*$pesotitolo+";
    $query2.="((tab2.title REGEXP '[[:<:]]".$Valore."[[:>:]]')>0)*$pesotitolo+";

    $query.="((tab1.text REGEXP '[[:<:]]".$Valore."[[:>:]]')>0)*$pesotesto+";
    $query2.="((tab2.text REGEXP '[[:<:]]".$Valore."[[:>:]]')>0)*$pesotesto+";

    $sqlwhere.="tab1.title REGEXP '[[:<:]]".$Valore."[[:>:]]' OR tab1.text REGEXP '[[:<:]]".$Valore."[[:>:]]' ";
    $sqlwhere2.="tab2.title REGEXP '[[:<:]]".$Valore."[[:>:]]' OR tab2.text REGEXP '[[:<:]]".$Valore."[[:>:]]' ";

    } else {
    $query.="(instr(tab1.title, '$Valore')>0)*$pesotitolo+";
    $query2.="(instr(tab2.title, '$Valore')>0)*$pesotitolo+";

    $query.="(instr(tab1.text, '$Valore')>0)*$pesotesto+";
    $query2.="(instr(tab2.text, '$Valore')>0)*$pesotesto+";

    $sqlwhere.="tab1.title like '%$Valore%' OR tab1.text like '%$Valore%' ";
    $sqlwhere2.="tab2.title like '%$Valore%' OR tab2.text like '%$Valore%' ";
    }
    }
    }

    $sqlwhere=substr($sqlwhere, 0, strlen($sqlwhere)-4);
    // calcolo la percentuale di rilevanza --> rilevanza*100/$maxPoint

    if($st == "1")
    {
    $query.="0)*100/$maxPoint,2) as rilevanza, id FROM tab1";

    $query.=" WHERE $sqlwhere order by rilevanza DESC";
    }
    else if(
    $st == "2")
    {
    $query.="0)*100/$maxPoint,2) as rilevanza, tab1.id FROM tab1";
    $query2.="0)*100/$maxPoint,2) as rilevanza, tab2.id FROM tab2";

    $query.=" WHERE $sqlwhere";
    $query2.=" WHERE $sqlwhere2";

    $query.=" UNION $query2 order by rilevanza DESC";
    }

    return
    $query;
    }
    }

    Grazie mille

  9. #9
    Guest

    Predefinito

    Giusto per non andare a tentativi, che errore ti da?

    Puoi anche provare, al posto di eseguirla, a farti stampare la query ed analizzarla (se vuoi posta la query - quella stampata da php - che ti da proplemi).
    Ultima modifica di simpleticket : 26-08-2011 alle ore 17.59.59

  10. #10
    Guest

    Predefinito

    Ciao grazie per la risposta, questa è la query:

    Codice PHP:
    SELECT 'tabella1' as tabella, ROUND((((tab1.title REGEXP '[[:<:]]cerca[[:>:]]')>0)*5+((tab1.text REGEXP '[[:<:]]cerca[[:>:]]')>0)*3+0)*100/8,2) as rilevanza, tab1.id FROM tab1 WHERE tab1.title REGEXP '[[:<:]]cerca[[:>:]]' OR tab1.text REGEXP '[[:<:]]cerca[[:>: UNION SELECT 'tabella2' as tabella, ROUND((((tab2.title REGEXP '[[:<:]]cerca[[:>:]]')>0)*5+((tab2.text REGEXP '[[:<:]]cerca[[:>:]]')>0)*3+0)*100/8,2) as rilevanza, tab2.id FROM tab2 WHERE tab2.title REGEXP '[[:<:]]cerca[[:>:]]' OR tab2.text REGEXP '[[:<:]]cerca[[:>:]]' order by rilevanza DESC
    Mentre questo è l'errore:


    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 'programmi' as tabella, ROUND((((tab2.title REG' at line 1



    Il problema è che la query varia in base alle parole cercate
    Ultima modifica di nextonext : 26-08-2011 alle ore 18.15.55

  11. #11
    Guest

    Predefinito

    controlla da queste parti

    Codice PHP:
    REGEXP '[[:<:]]cerca[[:>: UNION SELECT
    pare che non chiudi bene REGEXP

  12. #12
    Guest

    Predefinito

    Non capisco, nello script lo chiudo correttamente

  13. #13
    Guest

    Predefinito

    ma visto che nel codice chiami portali e programmi le due tabelle, e poi mi scrivi una query con tabella1 e tabella2, suppongo che sia saltato qualcosa.

    Prova a mettere un echo $query prima di mysql_query e analizza quello che viene scritto

  14. #14
    Guest

    Predefinito

    Avevo già messo l'eco query ed è quella che ti ho stampato.
    Per il secondo problema non ho corretto bene il codice:

    portali = tabella1
    programmi = tabella2

  15. #15
    Guest

    Predefinito

    Il problema sembrerebbe qui

    Codice PHP:
    $sqlwhere=substr($sqlwhere, 0, strlen($sqlwhere)-4);
    alla strina delle condizioni taglia gli ultimi 4 caratteri e quini ti tronca il testo come ti facevo notare prima.

    Prova a mettere delle condizioni e delle verifiche per valutare se e quando effettuare il trim degli ulimi caratteri.

  16. #16
    Guest

    Predefinito

    Ho risolto il problema, avevo creato le due stringe:
    $sqlwhere e $sqlwhere2 solo che ne tagliavo solo una...

    Grazie

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •