2013-06-12 97 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

没错!谢谢,这个网站上的人更关心的是减少问题而不是帮助。再次感谢你。 –