简体   繁体   English

在SQL中使用临时表来更新特定列并输出结果

[英]Using temporary table in SQL to UPDATE a certain column and output results

I have a table in my database which describes a list of different subjects including times the classes are. 我的数据库中有一张表,该表描述了不同主题的列表,包括上课的时间。 So the columns in my PostgreSQL database are: 所以我的PostgreSQL数据库中的列是:

subject_id (type: serial) 
subject (type: character varying)
class start times (type: time without time zone)

When I write my query to list all the subjects I would like to be able to modify the class times for a specific subject. 当我编写查询以列出所有主题时,我希望能够修改特定主题的上课时间。 I would like this change to be not permanent but temporary. 我希望这种变化不是永久的,而是暂时的。 I understand that I will need to make use of a temporary table in SQL but how can I select everything within my subjects table and make the temporary change of the class times to output to the user. 我知道我将需要使用SQL中的临时表,但是如何选择主题表中的所有内容并临时更改类时间以输出给用户。 Will I need to first make a temporary subjects tables, use this within my query and then run the UPDATE statement to change the time of a specific class, show this to the user and then DROP the table once it has been done. 我是否需要首先创建一个临时主题表,在查询中使用它,然后运行UPDATE语句以更改特定类的时间,向用户显示该时间,然后在完成后将其删除。 I understand what needs to be done but I am a little unsure of writing this up in the query. 我知道需要做什么,但是我不太确定在查询中将其编写出来。

This question might be clearer if you explained what you were trying to achieve. 如果您解释了要达到的目标,那么这个问题可能会更清楚。 Why does the start time change for different users? 为什么开始时间会因不同的用户而改变?

If you are modifying the start time, for example, to appear in a different time zone, postgres has this ability natively. 例如,如果您要修改开始时间以显示在其他时区中,则postgres本身具有此功能。 eg SELECT subject, class_start_time::timestamptz AT TIME ZONE UTC FROM subjects as s ; 例如SELECT subject, class_start_time::timestamptz AT TIME ZONE UTC FROM subjects as s ;

If you are trying to add or subtract some arbitrary amount of time, you can do that as well: SELECT subject, class_start_time + interval '1 hour' FROM subjects as s ; 如果要尝试增加或减去任意时间,也可以这样做: SELECT subject, class_start_time + interval '1 hour' FROM subjects as s ;

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

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