简体   繁体   English

从选择语句的另一列中将新列添加到mysql中

[英]Add new column into mysql from another column from select statement

I have a datetime() column date_time in my database. 我的数据库中有一个datetime()date_time

I need to convert this to date() and create a new temporary column date_field till the session ends . 我需要将其转换为date()并创建一个新的临时列date_field 直到会话结束

I've tried this: 我已经试过了:

$query1 = SELECT date_time, date(date_time) as date_field FROM table_name....

This works and creates a temp column date_field with all the date values for the date_time field 这可以正常工作,并创建一个临时列date_field以及date_time字段的所有日期值

Problem : This only works for this query. 问题 :这仅适用于该查询。 I also have another query which searches for date_time field and thus results into a DB error Unknown field 'date_field' . 我也有另一个查询,它搜索date_time字段,因此导致DB错误Unknown field 'date_field'

$query2 = SELECT date_field FROM table_name
                                    | This table doesn't have date_field as a column

Can this field be created so that I can use this for a time till the session ends ? 可以创建此字段,以便我可以在会话结束之前使用它吗? as we do for temporary tables ? 像我们为临时表所做的一样?

Note : I can't use ALTER to add new column here due to code limitations. 注意 :由于代码限制,我无法在此处使用ALTER添加新列。

Any ideas ? 有任何想法吗 ?

Thanks! 谢谢!

In the first query, you dont really create any column, you just return aa column (date_time), run some function on it (date()) and give it an alias name (date_field). 在第一个查询中,您实际上并没有创建任何列,只是返回一个列(date_time),在其上运行某些函数(date()),并为其指定别名(date_field)。 This happens only in the result set, and abvioysly cant be used outside of this query. 这仅在结果集中发生,并且不能在此查询之外使用。

What you need to do is simply use the same in the second query: instead of select date field you need select date(date_time) as date_field 您需要做的就是在第二个查询中使用相同的内容:代替select date field您需要select date(date_time) as date_field

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

相关问题 PHP MYSQL-插入新记录,但需要另一个表中的select语句中的列-PHP中失败 - PHP MYSQL - insert a new record but requires column from select statement in another table - fails in php mysql-根据另一列的值组合选择一列 - mysql - select a column based on a combination of values from another column MySQL-通过参考表从另一个表中选择列 - MySQL - Select column from another table via a reference table 根据一个 select 语句中另一列的不同要求对一列求和多次? - Sum a column multiple times based on different requirements from another column in one select statement? MySQL从另一个表添加列(无参考ID) - Mysql add column from another table (no reference id) MySQL语句从所有行获取列的值,其中另一列的值=指定的值 - MySQL statement to get a column's value from all rows where another column's value = a specified value MySQL Select,多个表中具有相同名称的列,由具有相同名称的另一列排序 - MySQL Select, column with same name from multiple tables, order by another column with same name PHP / MySQL-从另一列等于值的列中选择RANDOM值 - PHP/MySQL - Select RANDOM value from column where another column equals value 从mysql准备好的语句中获取一列 - fetch one column from mysql prepared statement SQL 查询将值从一列添加到另一列作为新行一次 - SQL query add value from one column to another column as a new row just once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM