2013-02-13 16 views
6

Sto facendo la ricerca jolly con JDBC.Come cercare valori da più tabelle usando i caratteri jolly?

Il codice funziona correttamente quando si utilizza una singola tabella.

Quando provo a combinare più tabelle utilizzando una ricerca con caratteri jolly, il codice non funziona.

Ad esempio, l'utente può cercare qualsiasi parola chiave.

  1. se l'utente cerca "grillo", la parola chiave cricket è availble nella tabella sport
  2. se l'utente cerca "Windows 8", è disponibile nella tabella Software
  3. se l'utente cerca "google, yahoo", le parole chiave sono disponibili nella tabella sito

Ecco il valore di input dinamico:

where s1(cricket,windows 8,google) 

Nella sola ricerca tavolo Cerco "grillo" nella tabella di sport.

Ecco la mia domanda, che funziona bene:

"select * from sports WHERE feed LIKE '%" +s1 + "%'"; 

La mia domanda più tabelle non funziona.

"select * from product WHERE sitename LIKE '%"+s1+"%'" "OR 

    "select * from sports  WHERE sitename LIKE '%"+s1+"%'" " OR 

    "select * from website WHERE sitename LIKE '%"+s1+"%'" " OR 

    "select * from software WHERE sitename LIKE '%"+s1+"%'" "OR 

    "select * from other WHERE sitename LIKE '%"+s1+"%'" 

Cosa c'è di sbagliato con questo codice?

+0

Nella tua domanda singola dici dove preferisci il feed e nella tua multitable dove sitename è simile? – Lyrion

risposta

2

Provare a concatenare le singole dichiarazioni con un UNION anziché OR. Con questo è possibile concatenare diverse query SQL e tabelle con le stesse colonne. Per esempio:

"select * from product WHERE sitename LIKE '%"+s1+"%' UNION 

select * from sports WHERE sitename LIKE '%"+s1+"%' UNION 

select * from website WHERE sitename LIKE '%"+s1+"%' UNION 

select * from software WHERE sitename LIKE '%"+s1+"%' UNION 

select * from other WHERE sitename LIKE '%"+s1+"%'" ; 

Ma attenzione! Le tue singole tabelle devono avere le stesse colonne/colonne, altrimenti non funzionerà!
Forse meglio esempio per chiarire:

"select sitename, description from product WHERE sitename LIKE '%"+s1+"%' UNION 

select sitename, description from sports WHERE sitename LIKE '%"+s1+"%' UNION 

select sitename, description from website WHERE sitename LIKE '%"+s1+"%' UNION 

select sitename, description from software WHERE sitename LIKE '%"+s1+"%' UNION 

select sitename, text as description from other WHERE sitename LIKE '%"+s1+"%'" ; 

Come si può vedere nella dichiarazione 6, è possibile fare riferimento altre colonne per il risultato da loro rinominando, ma il ColumnCount deve essere lo stesso.

Spero che questo possa aiutarti!

Problemi correlati