dongzhi4239 2016-01-08 08:45
浏览 29

从select语句的另一列向mysql中添加新列

I have a datetime() column date_time in my database.

I need to convert this to date() and create a new temporary column date_field till the session ends.

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

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'.

$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.

Any ideas ?

Thanks!

  • 写回答

1条回答 默认 最新

  • duanke2012 2016-01-08 08:58
    关注

    In the first query, you dont really create any column, you just return a a column (date_time), run some function on it (date()) and give it an alias name (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

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)