簡體   English   中英

MySQL 獲得最多匹配列值

[英]MySQL get highest number of matching columns values

我有這種設置。

TABLE images;
id    url                                 name
1     http://imgur.com/image1.png         image1.png
2     http://imgur.com/image2.png         image2.png
3     http://imgur.com/image3.png         image3.png
4     http://imgur.com/image4.png         image4.png
TABLE colors;
image_id  hex
1         9a8166
1         9a8130
1         000000
1         cdd69a
1         9a5430
1         662730
1         000030
1         665466
2         665430
2         9aac66
2         002700
2         cdac66
2         cd819a
2         302730
2         300030
2         002730

我 go 如何獲取包含十六進制代碼列表的所有圖像?

如果我hex in ([list of hex codes])我將獲得幾乎所有圖像,因為它們很有可能在列表中至少有一種顏色。

我想獲取包含所有 colors 的圖像,然后我可以處理從列表或其他任何內容中獲取至少包含 n-1 colors 的圖像。

編輯:

所以在這種情況下

如果我搜索 [blue],我會得到圖像 1 和 2。

如果我搜索 [blue, red] 我會得到圖像 1 和 2。

如果我搜索 [blue, red, pink] 我會得到圖像 2。

將十六進制列表放入一個新的 list_of_hex_codes 表中,其中包含一列十六進制(當然還有一個 id)。 假設有 n 個十六進制數,因此該表中將有 n 行。

創建查詢count(a.image_id) c, a.*, b.* from colors a, list_of_hex_codes b where a.hex = b.hex order by c

如果查詢中 a.image_id 的計數為 n -> 這意味着圖像包含整個十六進制代碼列表。 如果查詢中 a.image_id 的計數為 (n - 1) -> 這意味着圖像包含 n - 1 colors。依此類推。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM