0

I have a table name video

table video

id  name   upload_date

1   one     1408336348
2   two     1409884215

now i want to select all data also calculate if video uploaded between last 2 days then yes or no

result like

 id  name   upload_date   new

1   one     1408336348    no
2   two     1409884215    yes

I am using this query but not work

SELECT v.*,( if(from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY) then 'no' else 'yes') 
 AS new FROM `video` as v

Query return error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') then 'no' else 'yes') 
AS new FROM `video` as v
LIMIT 0, 25' at line 1 
2
  • What does your query return? Commented Sep 4, 2014 at 19:01
  • 1
    What means "not work?". Any errors or wrong result? Commented Sep 4, 2014 at 19:02

3 Answers 3

1

That's not the correct syntax for an IF in MySQL. Try it like this:

SELECT v.*,
    IF(from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY, 'yes', 'no') AS new
FROM `video` as v
Sign up to request clarification or add additional context in comments.

Comments

0

Try using case

SELECT v.*
    ,case
        when (from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY) then 'yes'
        else 'no')
     end AS new
FROM `video` as v

Comments

0

Try a CASE statement:

SELECT v.*, 
    CASE WHEN from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY 
         THEN 'Yes' 
         ELSE 'No' 
    END AS new 
FROM `video` 

This is the ansi-standard way to do it in SQL.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.