Mysql Date from Null to 00-00-0000 - php

Well I have a weird problem with MySQL.
I have a table like this :
# table_name #
==============
ID | DATE | RESULT_DATE
When I do
SELECT * FROM table_name
the output show me the table_name.RESULT_DATE as NULL or if there is a date, it show me the date.
BUT, when I do something like this :
SELECT * FROM table_name WHERE DATE BETWEEN '2017-05-03' AND '2017-05-08'
the output show me the table_name.RESULT_DATE as 00-00-0000 for ALL the elements, if they were NULL or if they had a Date element...
Why is this happening?
Thanks

You forgot the quotes around the dates
SELECT * FROM table_name WHERE DATE BETWEEN '2017-05-03' AND '2017-05-08'
here------------------^----------^-----^----------^

Related

How can I union 2 tables in PHP & MySQL?

I want to do 'union all' 2 tables. Because of this, I wrote the code below:
$query = "SELECT * FROM (SELECT * FROM news WHERE site_id = '1'
UNION ALL
SELECT * FROM all_news)
ORDER BY date DESC";
$news = mysqli_query($con, $query);
while($new = mysqli_fetch_object($news))
{
echo '- ' . $new->baslik . '<br>';
}
However, I got the error like that:
Warning: mysqli_fetch_object() expects parameter 1 to be
mysqli_result, boolean given in /../XAMPP/../../../index.php on line
19
Line 19 is while's line...
How can I fix it? I want to union 2 table, order by date all limit 10...
EDIT:
news table:
id | site_id | title | date (timestamp)
all_news table:
id | title | date (timestamp)
EDIT:
The problem was solved. I learned, if we want to use union or union all, our tables have to have same column number.
To union these 2 tables you will have to add an extra dummy column to the Query of the shorter table so the rules of UNION are met.
You dont need to add a real column to the schema for that table!
So if the tables are
News table:
id | site_id | title | date (timestamp)
all_news table:
id | title | date (timestamp)
Then the query can be written like this
SELECT id, site_id, title, date FROM news WHERE site_id = '1'
UNION ALL
SELECT id, 0, title, date FROM all_news
Note I added a dummy column to the select list of the all_news table so that the column count and the datatypes of each column match each other.
You can use any number you like other than 0, so pick one that identifies this as data you dont want to process, or it can be used to identify the rows from all_news by setting a specific value.
Note: I assumed site_id was an integer, if its a text then use '0' for example or 'IGNORE ME'.

Unable to get "inbetween" records if year is different

I am having this unique bug
TABLE : tbl
id | title | iscancel | sold_dt
id: UID
Title : varchar
iscancel : 0/1
sold_dt : timestamp
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "06/01/2015" AND "03/01/2016" GROUP BY day(sold_dt) order by (sold_dt) asc
(note that year is different)
0 records returned
but if i do
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "06/01/2015" AND "12/01/2015" GROUP BY day(sold_dt) order by (sold_dt) asc
or
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "01/01/2016" AND "03/01/2016" GROUP BY day(sold_dt) order by (sold_dt) asc
(note that year is same)
-I'll get some records
*Used DATE_FORMAT because my calendar is giving me MM/DD/YYYY (i can not change since it might affect other areas)
what i am doing wrong?
Instead of converting the stored value into another format try changing the parameter itself to correct format. If you cant do it on your website you can do it in your query as well
sold_dt BETWEEN
STR_TO_DATE('06/01/2015', '%m/%d/%Y') AND STR_TO_DATE('03/01/2016', '%m/%d/%Y')
Try by passing static dates into DATE_FORMAT() function
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN DATE_FORMAT("06/01/2015","%m/%d/%Y") AND DATE_FORMAT("03/01/2016","%m/%d/%Y") GROUP BY day(sold_dt) order by (sold_dt) asc

mysql, use condition in "where"

I have a database table like:
id name date price
1 a 2014-05-12 10
2 a 0000-00-00 20
3 a 2014-05-13 30
I want to search by date, and if the date exists, return the price of the date.
if the date does not exist, return the price of date 0000-00-00.
For example:
search by date:2014-05-12, return `10`
search by date:2014-05-20, return `30`
I have tried:
select price from table where (date=table.date or table.date='0000-00-00')
but it does not work.
How do I write the correct query?
Here is one method that uses order by and limit:
select t.price
from table t
where t.date in ('0000-00-00', #date)
order by t.date desc
limit 1;
Note that I changed the parameter name from date to #date to distinguish it from the column with the same name.
The syntax should table table.field (e.g. table.date), while in your example you have field.field (price.date).
Try:
select price from table where (table.date='{$date}' or table.date='0000-00-00')
Note that $date is a variable (assuming you use PHP). If you don't use PHP, you can't just write date as a variable here, but you can use #date.
Get the value of the field to be searched.Let it be $date_search.
if(!empty($date_search)) {
$sql = 'select price from table where date ='.$date_search;
mysql_query($sql);
}
else {
$sql = 'select price from table where date ="0000-00-00"';
mysql_query($sql);
}

mysql with the date function

I have a mysql table containing a field name dtt_date and have values like
08/04/2010 22:15:00. I want to display all the records with in this month (08, august), How to write a mysql query in my php page to display these record.
Does any one know this?
Please help me?
SELECT * FROM table WHERE dtt_date>='2010-08-01' AND dtt_date<='2010-08-31';
In PHP:
$q = "SELECT * FROM table WHERE dtt_date>='2010-08-01' AND dtt_date<='2010-08-31'";
$res = mysql_query($q);
while($row = mysql_fetch_assoc($res))
var_dump($row);
mysql_free_results($res);
Untested, I'm sure there are easier methods to this. Not sure if your date format will be handeled by MySQL
SELECT * FROM table WHERE MONTH(DATE_FORMAT(date,'%Y-%m-%d')) = 8
You can try this one method
SELECT * FROM table WHERE month(dtt_date)='08' AND year=(dtt_date)='2010';
You should change the date format in the table to be '2010-08-04 22:15:00', then you could run this query:
SELECT DATE_FORMAT(dtt_date, '%D %M %Y') as date FROM myTable
From there you would get something of this as a result, and then you can experiment with the date formatting.
+-----------------+
| date |
+-----------------+
| 4th August 2010 |
+-----------------+

How to use DATEDIFF? How many days are inside of two dates

How to use DATEDIFF? How can I make this to work? or should I use DATEDIFF completly differently?
SELECT DATEDIFF('Started ','will_end') AS 'Duration' FROM my_table WHERE id = '110';
I try to get answer, how many days are inside of two dates.
I would like to get an aswer like:
Duration = 7 days;
I have this kind of database:
Started | will_end
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26
Put will_end first, started second:
SELECT DATEDIFF('2009-12-24', '2009-12-17')
---
7
Also, remove the single quotes from your field names:
SELECT DATEDIFF(will_end, started) AS Duration
FROM my_table
WHERE id = 110
, or replace them with the backticks:
SELECT DATEDIFF(`will_end`, `started`) AS `Duration`
FROM `my_table`
WHERE `id` = 110
Are you getting a NULL result? You have the column names in single quotes in your query, which means you are passing the strings 'Started ' and 'will_end' to DATEDIFF rather than the column values. Try removing the single quotes, and you will start to see some results:
SELECT DATEDIFF(Started, will_end) AS Duration FROM my_table WHERE id = '110';
Note that this will give you a negative result. To get a positive result, reverse the order of the columns:
SELECT DATEDIFF(will_end, Started) AS Duration FROM my_table WHERE id = '110';
replace the order
DATEDIFF('will_end','Started')
I think there are 3 parameter to be passed in
DATEDIFF ( datepart , startdate , enddate )
so your code would be
DATEDIFF ( dd , 'Started ','will_end' )
http://msdn.microsoft.com/en-us/library/ms189794.aspx

Categories