首页 > 解决方案 > 如何从 SQLite3 中的多个表中查询图像

问题描述

我有一个包含以下表格的 SQlite 图像数据库:

CREATE TABLE crowd_labels (mid int, attribute text, label text, primary key (mid, attribute));

CREATE TABLE modules (
    mid int primary key,
    project_id int,
    src text
, mature_content, license);

CREATE TABLE scores (mid int,content_bicycle real, content_cat real, content_tree real, emotion_scary real, media_oilpaint real, content_bird real, content_dog real, emotion_gloomy real, media_3d_graphics real, media_pen_ink real, content_building real, content_flower real, emotion_happy real, media_comic real, media_vectorart real, content_cars real, content_people real, emotion_peaceful real, media_graphite real, media_watercolor real);

我可以单独执行以下查询,但我想知道如何将它们组合成一个查询:

sqlite3 mydatabase.sqlite <<EOF  > cat_pictures.htm
    select "<img src=""" || src || """ height=200>"
    from modules, crowd_labels where modules.mid = crowd_labels.mid
    and attribute = "content_cat"
    and label="positive"
    limit 100;
EOF

sqlite3 mydatabase.sqlite <<EOF  > watercolor_pictures.htm
    select "<img src=""" || src || """ height=200>"
    from modules, scores where modules.mid = scores.mid
    order by media_watercolor desc limit 0,100;
EOF

第一个查询将通过检查“crowd_labels”表返回一个包含 100 个猫图像的 html 文件,第二个查询将通过检查“scores”表返回一个包含 100 个水彩图像的 html 文件。

但是如果我想把两者结合起来,也就是把既是猫又是水彩的图像交集还给我,我该怎么做呢?

抱歉,如果这是一个非常简单的问题,但我在 SQL 方面的背景经验为 0。

标签: sqldatabasesqlite

解决方案


只需加入所有三个表:

SELECT '<img src="' || src || '" height=200>'
FROM modules
JOIN crowd_labels USING (mid)
JOIN scores       USING (mid)
WHERE attribute = 'content_cat'
  AND label     = 'positive'
ORDER BY media_watercolor DESC
LIMIT 100;

推荐阅读