mysql中更新并删除,在数据库mysql中更新和删除数据

I use codeigniter. i have following values as json_encode in database row, i want delete value 3 in row 1 and values 14 & 5 in database table.

DELETE -> value 3 on row 1 & values 14, 5 on rows 2

UPDATE -> value 2 to 8 on row 1 & value 3 to 17 on rows 2.

How is query it for values json?

Row 1:

id=1

[{

"hotel_id": ["3"]

}, {

"hotel_id": ["2"]

}, {

"hotel_id": ["1"]

}]

Row 2:

id=2

[{

"hotel_id": ["14"]

}, {

"hotel_id": ["5"]

}, {

"hotel_id": ["4"]

}, {

"hotel_id": ["3"]

}, {

"hotel_id": ["2"]

}, {

"hotel_id": ["1"]

}]

解决方案

What most of the commenters are trying to say is that your problem is a very common one when storing serialized data in the DB (like JSON strings) - That is exactly why this is considered bad practice.

All the wonderful tools of relational databases, like indexing, sorting, grouping and querying according to any column are gone once the data is stored as a big pile of characters the DB can't parse.

The solution is to save the actual DATA in the DB, and format it as JSON only on the application side, for use when needed.

In your case, you can create a table like hotels_to_users (I am doing some guesswork here about the meaning of your data, hoping you get it even if it's not exactly what you need). This table will look like:

user_id (?) | hotel_id

1 | 1

1 | 2

2 | 14

This will make it very easy to query, update, insert and delete any specific hotel to any user.

Hope this makes sense and that you are able to make that change, otherwise your problem can be solved with either some MySQL string functions like REPLACE and SUBSTR, or by doing all the work in the application code and saving a new JSON when done. Either way, maintaining it will be hard and require more effort in the long term.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值