普通网友 2012-08-22 18:01
浏览 118
已采纳

MySQL:更新第一个可用列为空的行

I have a table that I use to keep track of some associations between users and various other aspects of their website.

I need to be able to get the first available row and update one or two of it's columns ... the criteria is whether or not the user_id column has been used or not.

id | tag_id | user_id | product_id

If a row has a tag available where there is no user_id assigned, I want to be able to use and update that row for the latest purchased product.

1 | 100001 | 29 | 66  
2 | 100002 | 0  | 0 
3 | 100003 | 0  | 0

So as you can see, the second row would be the first eligible candidate.

I'm just not sure what the SQL needs to be in order to make that happen

  • 写回答

3条回答 默认 最新

  • doulei8475 2012-08-22 18:46
    关注
    UPDATE yourTablename SET user_id = 'your value for userid',
    product_id='ur value for productid' WHERE id=(select min(id) where user_id='0');
    

    alternative method already told are efficient but if your table has sorting with id

    UPDATE yourTablename SET user_id = 'your value for userid',
    product_id='ur value for productid' where user_id='0' LIMIT 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测