我目前正在使用以下查询将两个表连接在一起:
select distinct book_id, clean_page_content as page_content, 'clean' as status, reg_date
from book_db_clean_pages t
where book_id = 'sarafi_book_5'
and reg_date = '2015-08-26'
UNION all
select distinct book_id, dirty_page_content as page_content, 'dirty' as status, reg_date
from book_db_dirty_pages e
where book_id = 'sarafi_book_5'
and reg_date = '2015-08-26'
但是,当我想要寻找不同的 book_id 时,我需要更改两个子句(一个在上面,一个在 UNION 下面)。同样的原则适用于 reg_date 。所以我想知道是否可以简单地使用像 join 这样的命令来简化我的查询。原则上,我正在寻找之类的解决方案:
select distinct t.book_id, (t.clean_page_content or e.dirty_page_content) as page_content,
('clean' or 'dirty') as status, t.reg_date
from book_db_clean_pages t, book_db_dirty_pages e
where t.book_id = e.book_id and t.reg_date = e.reg_date
and t.book_id = 'sarafi_book_5'
and t.reg_date = '2015-08-26'
答案 0 :(得分:2)
将你的联合放入派生表:
a href="{{ app.request.getSchemeAndHttpHost() }}/contact"
所有现代DBMS都足够聪明,可以将条件推送到派生表中,因此在您执行此操作时不会出现性能损失。
顺便说一句:如果select *
from (
select ...
from book_db_clean_pages t
-- no where clause
union all
select ...
from book_db_dirty_pages e
where t.book_id = e.book_id and t.reg_date = e.reg_date
-- no where clause for book_id and reg_date
) t
where book_id = 'sarafi_book_5'
and reg_date = '2015-08-26';
是两个表中的主键(或唯一列),则在内部选择上应用book_id
将不会删除任何内容。
答案 1 :(得分:0)
您无法在编写查询时简化查询。
你可以写:
public void getDataBaseData() {
// /PawnStorescopy.db
if (!new File("/data/data/" + this.getPackageName()
+ "/PawnStorescopy.sqlite").exists()) {
try {
FileOutputStream out = new FileOutputStream("data/data/"
+ this.getPackageName() + "DatabaseName.sqlite");
InputStream in = getAssets().open("DatabaseName.db");
byte[] buffer = new byte[1024];
int readBytes = 0;
while ((readBytes = in.read(buffer)) != -1)
out.write(buffer, 0, readBytes);
in.close();
// out.close();
} catch (IOException e) {
}
}
SQLiteDatabase sqliteDB = SQLiteDatabase.openOrCreateDatabase(
"/data/data/" + this.getPackageName()
+ "/DatabaseName.sqlite", null);
Cursor cursor = sqliteDB.rawQuery("SELECT * FROM TableName", null);
if (cursor.moveToFirst()) {
do {
////Get Data
} while (cursor.moveToNext());
imageFilter(listDatabase);
}
}
您没有提到您正在使用的数据库。如果子查询可以使用索引,那么初始查询可能会更有效率。