This topic is locked
[SOLVED]

 Conditional query in sql

12/25/2011 10:18:35 PM
PHPRunner General questions
J
joiresende author

I have three fields, date of sale, purchase date and days in inventory. day where stock is the current date minus the date of purchase (datediff (Now (), purchased) AS Days inventory). I wanted to when the product is sold returns the word "sold" instead of days in stock.

sorry for my english

Sergey Kornilov admin 12/26/2011

If your database is MySQL you can use something like this:

select ...,

(IF InStock>0 THEN (datediff (Now(), purchased)

ELSE 'Sold'

END IF ) as `Days inventory`

from ...
J
joiresende author 12/26/2011



If your database is MySQL you can use something like this:

select ...,

(IF InStock>0 THEN (datediff (Now(), purchased)

ELSE 'Sold'

END IF ) as `Days inventory`

from ...



thanks for the help, but did not work

the fields of my table are these:

purchase_date

sales_date

days_stock

and that while sales_data is 0, days_stock = (datediff(Now(), purchase_date), and when greater than 0 = "sold"

how to do it with a sql query

thank you

C
cgphp 12/26/2011

Set the days_stock field as Custom (http://xlinesoft.com/phprunner/docs/_view_as__settings_custom.htm) and enter this code:

if($data['sales_date'] == 0)

{

$value = (strtotime(now()) - strtotime($data['purchase_date'])) / (3600 * 24);

}

else

{

$value = "sold";

}