Anda harus menormalkan ini.
3 Tabel:
Image {image_id, name}
Colors {color_id, red, green, blue, alpha}
ImageHasColor {image_id, color_id, number_of_times_appeared}
Memasukkan data harus sederhana, gunakan ...insert_id
berfungsi untuk mendapatkan id dari baris yang baru saja Anda masukkan.
Pilih dengan gabungan seperti:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
ORDER BY i.image_id
Periksa tautan ini tentang cara mengonversi warna HEX ke nilai RGB:http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/
Cari top 10 gambar yang benar-benar merah:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
WHERE c.red > 200
AND c.green < 50
AND c. green < 50
ORDER BY h.number_of_times_appeared
LIMIT 10
Cari gambar yang agak hitam:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
WHERE c.red < 30
AND c.green < 30
AND c. green < 30
ORDER BY h.number_of_times_appeared
LIMIT 10