can i use temperary variable in where clause

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanjay123456
    New Member
    • Sep 2006
    • 125

    can i use temperary variable in where clause

    Dear friends,

    see the Following query -----

    [code=mysql]
    SELECT headline, Concat(EXTRACT( YEAR FROM datetime),'-',EXTRACT(MONTH FROM datetime),'-',DAYOFMONTH(da tetime)) as abc FROM news_news where catid='1' and abc>='2007-10-1' and abc<='2007-10-30'[/code]


    here abc is a temp variable can i use it in where clause in same query ?????

    here i excute this but it give

    #1054 - Unknown column 'abc' in 'where clause'

    plz tell me anyone is it possible or not ????
    Last edited by pbmods; Oct 27 '07, 01:27 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Sanjay.

    Please use CODE tags when posting source code:

    &#91;CODE=my sql]
    MySQL code goes here.
    &#91;/CODE]

    You can create MySQL variables using the SET keyword:
    [code=mysql]
    SET @variable = 'abc';
    [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by sanjay123456
      Dear friends,

      see the Following query -----

      [code=mysql]
      SELECT headline, Concat(EXTRACT( YEAR FROM datetime),'-',EXTRACT(MONTH FROM datetime),'-',DAYOFMONTH(da tetime)) as abc FROM news_news where catid='1' and abc>='2007-10-1' and abc<='2007-10-30'[/code]


      here abc is a temp variable can i use it in where clause in same query ?????

      here i excute this but it give

      #1054 - Unknown column 'abc' in 'where clause'

      plz tell me anyone is it possible or not ????
      In MySQL, names of temporary variable are not known at the time that the where clause is being parsed. Hence; that will not work. But this works fine (I have a column named "timestamp" which is a DATETIME type):[CODE=mysql]SELECT * FROM stations
      where `timestamp` between '2007-02-01' and '2007-02-13';[/CODE]For the desired dates: '2007-02-01' and '2007-02-12'
      Last edited by bartonc; Oct 27 '07, 03:42 PM.

      Comment

      Working...