【问题标题】:Update table and then delete row if not set it更新表,如果未设置则删除行
【发布时间】:2014-11-26 18:42:57
【问题描述】:

我有两张桌子:

users
-------
-id *
-name
- id_detail

details
-------
-id
-name
-id_user *

* FK

然后我将以下 json 插入到这些表中:

{id: 1, name, 'jhon',details: [
                                {id: 11, name: 'detail 1'},
                                {id: 22, name: 'detail 2'},
                                {id: 33, name: 'detail 3'}
                              ]
}

现在我编辑了 json,我想更新这些表:

{id: 1, name, 'jhon',details: [
                                {id: 11, name: 'detail 1'},
                                {id: 22, name: 'detail 2'}
                              ]
}

我用表'users'更新了json的第一级,{id:1,name,'jhon'},然后是第二级。 {id: 11, name: 'detail 1'}, {id: 22, name: 'detail 2'} with table 'details' 但是我如何删除(从 json 中删除){id: 33, name: 'detail 3'} ??哪个是最好的查询?

【问题讨论】:

  • DELETE FROM somelinkedtable WHERE user_id = 1 AND id NOT IN (11,22); 什么的,如果没有你的实际表格很难分辨/
  • 它有效!谢谢@Wrikken

标签: mysql sql json


【解决方案1】:

我不确定这是你的问题,但如果你需要从 Json 更新两个 MySQL 表,最好的方法是按照以下步骤操作:

  1. 从 Json 中读取您的 users.id,将其存储在一个变量中,称为 $userid
  2. details删除where id_user = $userid
  3. users删除where id = $userid
  4. 将Json数据插入usersdetails表中

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多