MySQL SELECT 在执行其他列检查后具有特定列值的单行
MySQL SELECT a single row having a certain column value after performing other column checks
我有一个 table 包含照片的元数据:照片属于哪个画廊,它属于哪个人(可以通过名称或每个画廊内分配的内部 ID 来识别人),是否或照片中的人没有戴普通眼镜或墨镜,或者两者都不戴:
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| id | gallery_id | person_id_in_gallery | person_name_id | wear_glasses | wear_sunglasses | image_filename |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 1 | 1 | 1 | NULL | 1 | 0 | xa.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 2 | 1 | 1 | NULL | 1 | 0 | xb.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 3 | 1 | 3 | NULL | 1 | 0 | xc.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 4 | 2 | 1 | NULL | 0 | 1 | ya.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 5 | 2 | 2 | NULL | 0 | 1 | yb.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 6 | 2 | 2 | NULL | 0 | 1 | yc.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 7 | 2 | 3 | NULL | 0 | 1 | yd.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 8 | 3 | NULL | 1 | 1 | 0 | za.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 9 | 3 | NULL | 1 | 1 | 0 | zb.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 10 | 3 | NULL | 2 | 0 | 1 | zc.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 11 | 3 | NULL | 2 | 0 | 1 | zd.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 12 | 3 | NULL | 3 | 0 | 0 | ze.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
现在,我的任务是获取包含佩戴任一 reg 的人的照片列表。眼镜或墨镜,但我只需要为整个 table 中的每个人找到一张这样的照片。它变得有点复杂,因为可以通过所有数据库中的全局 ID (person_name_id) 或每个画廊中的本地 ID (person_id_in_gallery) 来识别一个人。想要的查询结果如下:
+----------------+
| image_filename |
+----------------+
| xa.jpg |
+----------------+
| xc.jpg |
+----------------+
| ya.jpg |
+----------------+
| yb.jpg |
+----------------+
| yd.jpg |
+----------------+
| za.jpg |
+----------------+
| zc.jpg |
+----------------+
这是我用来获取所有带眼镜和太阳镜的照片的查询。
SELECT image_filename FROM photos_table WHERE (wear_glasses = 1 or wear_sunglasses = 1) and (person_id_in_gallery IS NOT NULL or person_name_id IS NOT NULL)
但是,我所有尝试使用 DISTINCT 和 GROUP BY 语句从每个独特的人那里挑选一张照片的尝试都失败了,非常感谢在这种情况下正确使用它们的帮助。
您可以尝试以下方法(已在 dbfiddle 上测试)
SELECT MIN(image_filename) AS image_filename
FROM photos_table
WHERE (wear_glasses = 1 or wear_sunglasses = 1)
AND (person_id_in_gallery IS NOT NULL or person_name_id IS NOT NULL)
GROUP BY gallery_id, person_id_in_gallery, person_name_id;
我有一个 table 包含照片的元数据:照片属于哪个画廊,它属于哪个人(可以通过名称或每个画廊内分配的内部 ID 来识别人),是否或照片中的人没有戴普通眼镜或墨镜,或者两者都不戴:
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| id | gallery_id | person_id_in_gallery | person_name_id | wear_glasses | wear_sunglasses | image_filename |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 1 | 1 | 1 | NULL | 1 | 0 | xa.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 2 | 1 | 1 | NULL | 1 | 0 | xb.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 3 | 1 | 3 | NULL | 1 | 0 | xc.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 4 | 2 | 1 | NULL | 0 | 1 | ya.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 5 | 2 | 2 | NULL | 0 | 1 | yb.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 6 | 2 | 2 | NULL | 0 | 1 | yc.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 7 | 2 | 3 | NULL | 0 | 1 | yd.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 8 | 3 | NULL | 1 | 1 | 0 | za.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 9 | 3 | NULL | 1 | 1 | 0 | zb.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 10 | 3 | NULL | 2 | 0 | 1 | zc.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 11 | 3 | NULL | 2 | 0 | 1 | zd.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 12 | 3 | NULL | 3 | 0 | 0 | ze.jpg |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
现在,我的任务是获取包含佩戴任一 reg 的人的照片列表。眼镜或墨镜,但我只需要为整个 table 中的每个人找到一张这样的照片。它变得有点复杂,因为可以通过所有数据库中的全局 ID (person_name_id) 或每个画廊中的本地 ID (person_id_in_gallery) 来识别一个人。想要的查询结果如下:
+----------------+
| image_filename |
+----------------+
| xa.jpg |
+----------------+
| xc.jpg |
+----------------+
| ya.jpg |
+----------------+
| yb.jpg |
+----------------+
| yd.jpg |
+----------------+
| za.jpg |
+----------------+
| zc.jpg |
+----------------+
这是我用来获取所有带眼镜和太阳镜的照片的查询。
SELECT image_filename FROM photos_table WHERE (wear_glasses = 1 or wear_sunglasses = 1) and (person_id_in_gallery IS NOT NULL or person_name_id IS NOT NULL)
但是,我所有尝试使用 DISTINCT 和 GROUP BY 语句从每个独特的人那里挑选一张照片的尝试都失败了,非常感谢在这种情况下正确使用它们的帮助。
您可以尝试以下方法(已在 dbfiddle 上测试)
SELECT MIN(image_filename) AS image_filename
FROM photos_table
WHERE (wear_glasses = 1 or wear_sunglasses = 1)
AND (person_id_in_gallery IS NOT NULL or person_name_id IS NOT NULL)
GROUP BY gallery_id, person_id_in_gallery, person_name_id;