简体   繁体   English

在Android中使用WHERE子句联接3个表

[英]Joining 3 tables with WHERE clause in Android

I have problem with SQL in Android (SQLite). 我在Android(SQLite)中遇到有关SQL的问题。 I have three tables in relation many-to-many, suppose A, B and C, each has 30 records. 我有关于多对多的三个表,假设A,B和C,每个表都有30条记录。 I want to join these tables and after that use WHERE clause. 我想加入这些表,然后使用WHERE子句。 I have written something like this code 我写了这样的代码

SELECT *
FROM A
   JOIN B ON A.id=B.photoid 
   JOIN C ON B.urlid=C.id 
WHERE C.type=[SOME VALUE]

I have also tried this 我也尝试过

SELECT *
FROM A
   LEFT OUTER JOIN B ON A.id=B.photoid 
   LEFT OUTER JOIN C ON B.urlid=C.id AND C.type=[SOME VALUE]

and this 和这个

SELECT *
FROM A
   CROSS JOIN B
   CROSS JOIN C
WHERE A.id=B.photoid AND B.urlid=C.id AND C.type=[SOME VALUE]

These statements don't work in Android. 这些陈述不适用于Android。 They return 300 records. 他们返回300条记录。 What should I change? 我应该改变什么? Is it bug or I do something wrong? 是错误还是我做错了什么?

Hard to tell without any additional data, but keep in mind that a JOIN operation will produce a row for each combination of a value in any of the tables involved. 没有任何其他数据很难说,但要记住, JOIN操作将为所涉及的任何表中的值的每个组合产生一行。 If you had 3 tables, each with 30 rows and you wouldn't be filtering any result, it should produce 27000 rows, but you're filtering by A.id=B.photoid and B.urlid=C.id , which might be producing a smaller set of rows. 如果您有3个表,每个表都有30行,并且您不会过滤任何结果,那么它应该会产生27000行,但是您正在按A.id=B.photoidB.urlid=C.id过滤产生一组较小的行。

In my opinion, that's not anything related to SQLite, but to your query instead. 我认为,这与SQLite无关,而与查询有关。 I'd try to make your set of values much smaller (for example, 5 per table) and run the same query, and see which results are being returned which shouldn't be, and try to modify your query to make it discard all those unwanted results. 我会尝试使您的值集更小(例如,每个表5个)并运行相同的查询,并查看返回的结果不应该返回,并尝试修改查询以使其放弃所有那些不必要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM