duan010167787 2013-11-04 13:26
浏览 9
已采纳

存储和调用单个列的时间戳(或常规数据)

I'm quite new to programming in general, so I expect this question to have an easy solution. I searched for an answer before posting, but I'm not really sure what I'm looking for.

Basically, I have a database with the following structure:

Table: things
Column 1- ID
Column 2- Name
Column 3- Year
Column 4- Timestamps

I have a large collection of timestamps for many things of the format hh:mm:ss that I want to store in the 4th column of the things table. Each item in the table will have a varying number of timestamps associated with it, so I thought it would make sense to simply store them all in a single column separated by commas (hh:mm:ss,hh:mm:ss) rather than store each timestamp in its own column. From here, I hoped that using PHP I could select the name of a thing and recall its year and timestamps, separating each timestamp into its own variable.

Example:

Column 2- Thing20
Column 3- 1997
Column 4- 00:01:24,00:05:28,00:16:52

$name = "Thing20"
$year = 1997
$ts1 = "00:01:24"
$ts2 = "00:05:28"
$ts3 = "00:16:52"

Here are my questions...

  1. Is this a practical solution for my needs? I'm having trouble thinking of a better way to do this...
  2. How would I go about separating the timestamps (which are separated by commas in the database entry) and storing them as incremental variables? Rather, I suppose you would use an array instead of many variables.

Any help at all would be greatly appreciated. As I said above, I am a beginner so I'm very sorry if this is a trivial thing to ask!

Thank you!

  • 写回答

2条回答 默认 最新

  • drzfz9995 2013-11-04 13:41
    关注

    Design issue

    If you need n values for each ID, then instead of storing it in a single column you should split it into two tables.

    Table 1: things (ID, name, year), where ID is a primary key

    Table 2: timestamps (ID, timestamp), where ID is a foreign key from 'things' and where you can store as many ID,timestamp pairs as you wish (and timestamp is a single value)

    So in your example, table 1 will look like this:

    ID, Name, Year
    1, Thing20, 1997
    

    Table 2 will look like this:

    ID, timestamp
    1, 00:01:24
    1, 00:05:28
    1, 00:16:52
    

    Practical solution

    However, if changing DB structure is not a case, then you can just use:

    $ts = explode(',', $timestamp);
    

    and you will receive an array of all timestamps in $ts variable

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功