我正在為影像建立一個類似火種的評級系統。
我需要找到與提供的影像具有最接近評級的影像。并且這對影像不存在于比較表中。
第一部分很簡單,看起來像這樣
SELECT images.file_path, images.id
FROM images JOIN ratings ON images.id = ratings.image
WHERE images.id != ? AND ratings.label = ?
ORDER BY abs(? - ratings.mu) ASC
然后我有一個包含以下資訊的表,我需要確保第一個查詢的結果不會產生該表中包含的對
img1: Integer - Image id
img2: Integer - Image id
outcome: Integer - Not relevant to question
現在我正在使用 SQLite 進行測驗,但是適用于多個 DB 的解決方案對于進一步的開發非常有用
示例 影像表(省略檔案路徑)
id, label, mu
1, 1, 1.5
2, 1, 2.0
3, 1, 2.5
4, 1, 3.5
比較表(結果省略)
img1, img2
1, 2
2, 3
3, 4
id 為 1 的影像的示例結果 = id 為 3 的 img。因為它的評分最接近 1.5 并且不能是影像 2,因為這對在比較表中
uj5u.com熱心網友回復:
您可以使用 aNOT EXISTS過濾掉不應該存在的內容。
WITH img AS ( SELECT id as image_id, label, mu FROM images WHERE id = 1 ) SELECT images.file_path , images.id as image_id , ratings.mu as rating_mu , ratings.label as rating_label FROM images CROSS JOIN img INNER JOIN ratings ON images.id = ratings.image AND ratings.label = img.label WHERE NOT EXISTS ( SELECT 1 FROM comparison c WHERE images.id in (c.img1, c. img2) AND img.image_id in (c.img1, c. img2) ) ORDER BY abs(img.mu - ratings.mu) ASC LIMIT 1;檔案路徑 | image_id | rating_mu | 評級標簽 :----------- | --------: | --------: | ------------: /img/foo.png | 3 | 2.5 | 1
在這里測驗db<>fiddle
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420110.html
標籤:
