简体   繁体   中英

Looking for mysql rows between certain unix time stamps

I am trying to write a mysql query that pulls in a unix timestamp converts it to %Y%m%d format then finds the rows between a certain date range. The query works until we get the WHERE clause. I have no idea why it isn't working. I was hoping some sql guru out here could help me.

Thanks,

Chris

SELECT 
            date_format(from_unixtime('timestamp'),'%Y%m%d') AS 'datecreated'
            ,`order_id`
            , `email`
            , `subtotal`
            , `shipping_cost`
            , `total`
            , `status`
        FROM
            `orders`
        WHERE 'datecreated' >= '20160501' AND 'datecreated' < '20160512'

You CANT use the alias on the where

Either you write the date_format function again

SELECT 
        date_format(from_unixtime('timestamp'),'%Y%m%d') AS `datecreated`
        ,`order_id`
        , `email`
        , `subtotal`
        , `shipping_cost`
        , `total`
        , `status`
    FROM
        `orders`
    WHERE date_format(from_unixtime('timestamp'),'%Y%m%d') >= '20160501' 
     AND  date_format(from_unixtime('timestamp'),'%Y%m%d') < '20160512'

Or create a sub query

SELECT *
FROM (
    SELECT 
        date_format(from_unixtime('timestamp'),'%Y%m%d') AS `datecreated`
        ,`order_id`
        , `email`
        , `subtotal`
        , `shipping_cost`
        , `total`
        , `status`
    FROM
        `orders`
     ) T
 WHERE `datecreated` >= '20160501' AND 'datecreated' < '20160512'

EDIT:

also fieldname use back thicks not single quotes

  `datecreated` field name
  'datecreated' string constant

I am trying to write a mysql query that pulls in a unix timestamp converts it to %Y%m%d format then finds the rows between a certain date range.

You might be thinking through the problem backwards. First find the right rows via WHERE, and then worry about formatting/converting the data in the result set:

SELECT date_format(from_unixtime('timestamp'),'%Y%m%d') AS "datecreated",
       ... other fields ...
  FROM "orders"
 WHERE "timestamp" >= UNIX_TIMESTAMP('2016-05-01 00:00:00')
       AND
       "timestamp" <  UNIX_TIMESTAMP('2016-05-12 00:00:00')

As a bonus, the above can make use of an index on "timestamp" .

Aside

In its default mode, MySQL allows several quoting styles and permits strings to be "double quoted" or 'single quoted' , even though the former is the ANSI SQL standard for quoting identifiers (like column names).

While that's a convenience, I think it's more trouble than it's worth in cases like this. Where you wrote 'col' > 'yyyymmdd' you were comparing one string to another instead of the computed column you intended to compare. (As mentioned in comments, that, too, would have failed, but your query didn't get that far.)

You might want to familiarize yourself with MySQL's ANSI mode which removes much of this ambiguity. You'll appreciate the practice if you ever move to an RDBMS other than MySQL.

change date format

SELECT 
            date_format(from_unixtime('timestamp'),'%Y%m%d') AS 'datecreated'
            ,`order_id`
            , `email`
            , `subtotal`
            , `shipping_cost`
            , `total`
            , `status`
        FROM
            `orders`
        WHERE from_unixtime('timestamp') between '2016-05-01' AND '2016-0512'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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