简体   繁体   English

使用数组创建 SQL 表

[英]Create an SQL table with arrays

I have a question!我有个问题! I don't really know how to explain it, so I'll illustrate it!我真的不知道怎么解释,所以我来说明一下!

I know how to make tables on SQL that will have this shape :我知道如何在 SQL 上制作具有这种形状的表格:

var data = {
  "news" :[
    {
      "title" : "I am the title 1",
      "number of views" : 28,
      "text" : "Here is the text",
      "date of publication" : "13 March"
    },
    {
      "title" : "I am the title 2",
      "number of views" : 57,
      "text" : "Here is the text",
      "date of publication" : "24 May"
      ]
    },
    {
      "title" : "I am the title 3",
      "number of views" : 74,
      "text" : "Here is the text",
      "date of publication" : "27 May"
    }
  ]
};

Now I would like to be able to create a table that will have this shape :现在我希望能够创建一个具有这种形状的表格:

var data = {
  "staff member" :[
     {
      "First name" : "John",
      "Last name" : "Doe",
      "schedules" : [
          "Friday : 14h - 22h",
          "Saturday : 10h - 19h",
          "Sunday : 10h - 19h"
      ]
    },
    {
      "First name" : "Amelia",
      "Last name" : "Doe",
      "schedules" : [
          "Friday : 18h - 23h",
          "Saturday : 9h - 17h",
          "Sunday : 8h - 20h"
      ]
    }
  ]
};

See the difference?看到不同? Sorry if it's a little confusing, but I don't really know what to call it!对不起,如果它有点混乱,但我真的不知道该怎么称呼它!

I think it can be done by creating another table and finding myself with a common id for the first name, the last name on one side and the schedules on the other.我认为这可以通过创建另一个表并找到自己的名字、姓氏和时间表的通用 ID 来完成。 But there must be a way to do it more simply, right?但一定有更简单的方法,对吧?

So my question is simple, is it possible?所以我的问题很简单,这可能吗? And if so, how?如果是这样,如何?

I did it in JSON, but now I'd like to switch to MySQL, and I don't know how to do that!我是用 JSON 做的,但现在我想切换到 MySQL,我不知道该怎么做!

Thank you for your answers.谢谢您的回答。

EDIT :编辑 :

The issue is about schedules and staff members.问题是关于时间表和工作人员。 So I have to create 2 different tables and link the IDs between them to have a staff members with his schedules?因此,我必须创建 2 个不同的表并将它们之间的 ID 链接起来,以便让工作人员与他的日程安排?

SQL does not support the concept of "sub tables" the way you are showing in your code. SQL 不支持您在代码中显示的“子表”概念。 Instead you should create two separate tables similar to:相反,您应该创建两个单独的表,类似于:

    CREATE TABLE staff_member (
        staff_member_id int,
        First_name varchar,
        Last_name varchar
    )

    CREATE TABLE schedules (
       staff_member_id int,
       day_of_week varchar,
       start_hour int,
       end_hour int
    )

Then you can select the data using:然后您可以使用以下方法选择数据:

   SELECT m.First_name, m.Last_name, s.day_of_week, s.start_hour, s.end_hour
      FROM staff_member m 
      LEFT JOIN schedules s ON m.staff_member_id = s.staff_member_id
      ORDER BY m.Last_name, m.First_name, s.day_of_week, s.start_hour, s.end_hour

Of course, you can add a WHERE clause to select specific information.当然,您可以添加WHERE子句来选择特定信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM