【问题标题】:How to upload array of strings in mysql如何在mysql中上传字符串数组
【发布时间】:2020-09-25 21:48:01
【问题描述】:

我想在我的产品表中插入字符串数组,我是这个 mysql 的新手。就像在 mongodb 中一样,我这样做是为了存储照片列表

const productSchema = mongoose.Schema({
    name : {type:String,trim : true},
    preview : String,
    photos : [String],
    description : String,
    isAccessory : Boolean,
    brand : String,
    price : Number

})

这是我的请求正文

{
        "id": "1",
        "name": "Men Navy Blue Solid Sweatshirt",
        "preview": "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/08a7b230-ee8f-46c0-a945-4e835a3c01c01541402833619-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-1.jpg",
        "photos": [
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/08a7b230-ee8f-46c0-a945-4e835a3c01c01541402833619-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-1.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/efc3d5b9-1bb3-4427-af53-7acae7af98951541402833591-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-2.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/c7e58861-3431-4189-9903-9880f5eebd181541402833566-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-3.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/66490b64-32de-44b4-a6e4-fe36f1c040051541402833548-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-4.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/957be784-7c5d-4e90-ab9f-0928015b22891541402833645-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-5.jpg"
        ],
        "description": "Navy solid sweatshirt with patchwork, has a round neck, long sleeves, straight hem",
        "isAccessory": false,
        "brand": "United Colors of Benetton",
        "price": 2599
    },

现在我怎样才能在 sql 中做到这一点,就像它的表设计一样。

【问题讨论】:

  • 这能回答你的问题吗? How to store arrays in MySQL?
  • 好吧,即使我可以创建另一个表,我如何在该表中插入这组照片,就像我必须使用循环一样。我正在考虑的另一个解决方案是将 json.stringify 数组存储在我的照片列中,而不是创建新表,那么哪一个应该是正确的方法?@Mostafiz Rahman

标签: mysql node.js mongodb


【解决方案1】:

根据我的理解,我可以想到两种可能的选择:

Option-1
MySQL supports storing JSON. 将字符串数组存储为所需键的值。

选项 2
使用JSON.stringify 存储字符串值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2014-08-17
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多