执行其他列检查后,MySQL选择具有特定列值的单行
我有一张包含照片元数据的桌子:哪个画廊照片属于哪个人(可以用名称来识别人,也可以在每个画廊内部分配的内部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的人的照片列表。眼镜或深色眼镜,但我只需要找到整个桌子上每个人的照片。它会变得有些复杂,因为在所有数据库(Person_Name_ID)或每个画廊中的本地ID(Person_ID_IN_Gallery)中,可以通过全局ID识别一个人。所需的查询结果如下:
+----------------+
| 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)
但是,我所有尝试使用不同的陈述使用不同的尝试来选择每个独特人的照片失败的尝试,我非常感谢在这种情况下正确使用它们的帮助。
I have a table containing metadata of photos: which gallery photo belongs to, which person it belongs to (person could be identified either by name, or by internal id assigned inside each gallery), whether or not a person in a photo wears regular glasses or dark glasses, or neither:
+----+------------+----------------------+------------------+----------------+------------------+----------------+
| 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 |
+----+------------+----------------------+------------------+----------------+------------------+----------------+
Now, my task is to obtain a list of photos that contain a person wearing either reg. glasses or dark glasses, but I need to only find one such photo of EACH of the people in the whole table. It gets slightly complicated, because a person could be identified either by global id throughout all databases (person_name_id), or by a local id in each gallery (person_id_in_gallery). The desired query result is as follows:
+----------------+
| image_filename |
+----------------+
| xa.jpg |
+----------------+
| xc.jpg |
+----------------+
| ya.jpg |
+----------------+
| yb.jpg |
+----------------+
| yd.jpg |
+----------------+
| za.jpg |
+----------------+
| zc.jpg |
+----------------+
Here is a query I used to obtain all the photos with glasses and sunglasses.
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)
However, all my attempts to use DISTINCT and GROUP BY statements to pick a single photo from each unique person failed, and I would greatly appreciate help with using them properly in this scenario.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试以下(在
You can try the following (tested on dbfiddle)