2013-06-12 36 views
-2

我有3個表,它們在SQLite數據庫中沒有任何關係。查詢返回所有表字段後,檢查3個表是否存在值的SQL查詢

我不得不返回有關3桌在那裏我有一個條件,像這樣的東西之一的所有字段:

(table1.field1="something" and table1.field2="something") OR (table2.filed1="something" and table2.field2="something") ... 

所以我想知道什麼表有場「東西」而歸該表的其他領域與這些信息。

如果問題根本不明確,我會盡力解決問題。

編輯:

我想是這樣的:

SELECT idpontosturisticos,idrestauracao,idalojamento from Alojamento,pontosturisticos ,restauracao WHERE (alojamento.latitude=41.158764 AND alojamento.longitude=-7.784906 AND alojamento.nome='Restaurante1') OR (pontosturisticos.latitude=41.158764 AND pontosturisticos.longitude=-7.784906 AND pontosturisticos.nome='Restaurante1') OR (restauracao.latitude=41.158764 AND restauracao.longitude=-7.784906 AND restauracao.nome='Restaurante1') 
+2

請提供樣本輸入,樣本輸出並告訴我們[你試過的東西](http:// whathaveyoutried .COM)。 – 2013-06-12 16:44:38

+0

這些表格是否有多行?如果是這樣,你將如何決定哪些行進行比較,因爲你的表格(正如你指出的那樣)*沒有它們之間的任何關係*? –

+0

我用一個我試過的例子來編輯帖子。 –

回答

1

我覺得你只是想要一個UNION。

像這樣:

SELECT idpontasturisticos, otherfielda1, otherfielda2, otherfieda3 from pontosturisticos WHERE (latitude=41.158764 AND longitude=-7.784906 AND nome='Restaurante1') 
UNION 
SELECT idrestauracao, otherfieldb1, otherfieldb2, otherfiedb3 from restauracao WHERE (latitude=41.158764 AND longitude=-7.784906 AND nome='Restaurante1') 
UNION 
SELECT idalojamento, otherfieldc1, otherfieldc2, otherfiedc3 from alojamento WHERE (latitude=41.158764 AND longitude=-7.784906 AND nome='Restaurante1') 

你只需要確保你從每三個子查詢中檢索相同的字段數。這些字段不必是相同的名稱,但它們都會在同一列中出現。列名將是來自第一個子查詢的名稱(在這種情況下,是idpontasturistocs,otherfieda1,otherfielda2,otherfielda3)

+0

沒錯!謝謝,這個網站上的人更關心的是減少問題而不是幫助。再次感謝你。 –