教义:如何使用 SELECT MIN 子查询进行 UPDATE?
我有一个照片表,用户可以在其中拥有多张照片。
我正在尝试创建以下查询:
$q = Doctrine_Query::create()
->update('Photo p')
->set('p.photo_type', 1)
->where('p.user_id = ?', $id)
->andWhere('p.date_added = (SELECT MIN(p.date_added) FROM Photo p WHERE p.user_id = ?)', $id)
想法是将添加了最小日期的该用户的特定照片(最早的照片)的“photo_type”设置为 1。
我似乎无法正确理解语法。有人能指出我正确的方向吗?
谢谢。
编辑:
根据这个问题的答案(MySQL 错误 1093 - 无法在 FROM 子句中指定要更新的目标表)。这个问题可以结束了。
I have a photos table where users can have multiple photos.
I'm trying to create the following query:
$q = Doctrine_Query::create()
->update('Photo p')
->set('p.photo_type', 1)
->where('p.user_id = ?', $id)
->andWhere('p.date_added = (SELECT MIN(p.date_added) FROM Photo p WHERE p.user_id = ?)', $id)
The idea is to set the "photo_type" to 1 for that specific photo of this user that has the minimum date added (earliest photo).
I just can't seem to get the syntax right. Can someone point me in the right direction?
Thank you.
EDIT:
It seems I'm trying to do something that can't be done, as per the answer to this question (MySQL Error 1093 - Can't specify target table for update in FROM clause). This question can be closed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: