mysql select with priority of 3 filelds in a query - mysql

my table and fields are like these:
i must find $sy<year<$ey then it must filter only values by $sm<month<$em at last it must find $sd<day<$ed
i need to find records between dates for example like 2010/10/25 , 2010/10/10
at first i tried :
SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;
but it cant find records for dates like e like 2010/10/25 , 2010/10/28
than i tried
SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
this isnt usful in my case!
i need some thing like priority select first select all between years than month and than day!!
other way is convert mysql records to timestamp by year and month and day and compare it by input date but UNIX_TIMESTAMP('year-month-day 00:00:00') dont worked correct for me.
i used it like :
SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453

If convert to timestamp didn't work for you what about use date_format to convert:
SELECT *
FROM `work_result`
WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') >
DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')

Related

Select the latest year in a column using SQL query

I have a dataframe like this
Value.
Date
A
08/08/2009
A
09/12/2021
A
05/10/2022
A
06/09/2022
A
07/08/2022
I need output like
VALUE
DATE
A
05/10/2022
A
06/09/2022
A
07/08/2022
We have to print a latest year with all month data present in the date column .please refer output table.
i used SQL query like
Select Top 10 * from table where
Order by (Date) DESC;
The max() select only one date so that didn't help me
But didn't get expected answer.
Can please someone help me with the query ?
You can just use MAX in a subquery, this will produce the intended outcome you have shown in your question:
SELECT yourcolumn
FROM yourtable
WHERE
YEAR(yourcolumn) = (SELECT MAX(YEAR(yourcolumn)) FROM yourtable);
The latest year is 2022, so MAX in the subquery will find this year and the whole query will select all dates in 2022.
SELECT *
FROM tablename
WHERE datecolumn >= (SELECT DATE_FORMAT(MAX(datecolumn), '%Y-01-01')
FROM tablename)
To improve this query you'd have an index by datecolumn (or where this column is an expression prefix).

Get date time in correct format from SQL query

I have a column ifd0_DateTime , in a table named photo, which contains date time in following format: 1966:12:22 17:19:57.
I need to count the number of photos month wise for every year.
So far I have this query. but it doesn't work correctly.
SELECT ifd0_DateTime, count(*) FROM photo
group by YEAR(ifd0_DateTime), MONTH(ifd0_DateTime);
Could anyone please fix this ?
SELECT STR_TO_DATE(LEFT(ifd0_DateTime, 7), '%Y:%m'),
COUNT(*) AS dateCnt
FROM photo
GROUP BY STR_TO_DATE(LEFT(ifd0_DateTime, 7), '%Y:%m')
Instead of date select the Year and Month
Try this
SELECT YEAR(ifd0_DateTime), MONTH(ifd0_DateTime), count(*)
FROM photo
group by YEAR(ifd0_DateTime), MONTH(ifd0_DateTime)
The proper way to store a date time is using the built-in data types. However, it looks like you are storing the value as a string. If so, you just want the first 7 characters:
SELECT LEFT(ifd0_DateTime, 7) as yyyymm, count(*)
FROM photo
GROUP BY LEFT(ifd0_DateTime, 7)
ORDER BY yyyymm;
However, you really should fix the data.

How to select rows in a date table? My search operation is, select rows which lies between current date and 7 days before current date?

How to select rows in a date table using MYSQL? My search operation is, select rows which lies between current date and 7 days before current date?
SELECT * FROM {{table_name}}
WHERE {{date_field}} BETWEEN {{date_7_days_ago}} AND {{date_now}}
Obtaining the date depends on the programming language you're working with.
<mood sympathy="on">
Try this:
select *
from mytable
where my_date_column between SUBDATE(CURDATE(), 7) and CURDATE()

Mysql Select with Dates and maybe Case when

im having a problem where i cant think of a solution, maybe im having a bad table-structure or i just dont know enough about mysql select commands to think of a good solution. Maybe you can help me out:
So i got a table that has a Column with the Date-format (yyyy-mm-dd) i wanted to select all upcoming dates so i did:
SELECT * WHERE date >= now.
This worked kinda well but i also got "dates" where only the year is entered (2014-00-00) i also wanted to select these but "now" is already bigger so i made another column with the year only and if the month, date or both arent known i will use 0000-00-00 and the Column "year" now i could select like this:
SELECT * WHERE date >= now AND year >=now(year)
Now all entrys with 0000-00-00 wont be selected. If i use OR the entrys from last year will be shown.
So thats my problem, is there any way i can change my table so i can have entries with only the year or only year and month and of course all together? I already considered get rid of the date-format and use simple INT with seperated columns for year, month and date. But i think i will have the same problem.
Sometimes i just want to do a capsuled select like
SELECT *
WHERE (date >= now AND year >= now(year))
OR date == "0000-00-00" (i know that this doesnt work)
If I understood your problem correctly, you could use this request:
WHERE (date >= now OR year > now(year))
There is probably a simpler way though, that would preserve your design, like initializing at January 1st (01-01) instead of 00-00
I think you can use this code:
$_SESSION['month'] = //set here your selected month
$_SESSION['year'] = //set here your selected year
SELECT * FROM table WHERE DATEPART(m,date) >= '".$_SESSION['month']."' AND DATEPART(yyyy,year) >= '".$_SESSION['year']."' AND date <> '0000-00-00'
Change your table structure format. Actually just allow for that field to have null value when not entered. By default it will be null then. You shouldn't be storing 0000-00-00 as a value for Date type field. I would rather leave it as null , or as suggested in some of previous answers, initialize it with some other date. It would be much easier to manipulate with database then.
the problem is that half of you write is not MySQL and your database schema is terrible...
You have the following problems:
column data date does not have the date data type.
To fix it, you need to add a cast to the select statement eg. cast(datecolumn as date)
select * from table where cast(datecolumn as date) >= '2014-01-10';
the way to use now date is using the now function.
select now(), date(now());
result> 2014-01-10 11:11:36, 2014-01-10
select * from table where cast(datecolumn as date) >= date(now());
Because your datecolumn is not a date (2014-00-00 is not a valid date), you need to use string manipulation to extract the year.
select substring('2014-01-01', 1,4)
result> 2014
select * from table where substring(datecolumn, 1,4) = year(now());
The comparassion operator is = and not ==
the select statement syntax looks like this (pay attention because you are missing the table in your statement)
select * from [Table] where [column] = condition ...
You probably need or instead of ands, therefore your query should look like this:
select * from FooTable where
cast(datecolumn as date) >= date(now())
or substring(datecolumn, 1,4) >= year(now())
or datecolumn = '0000-00-00'
You should use something like phpmyAdmin or mySQL workbench to test your sql queries before try to use them on php, java or whatever is your programing language.

Year change not give result in mysql server

In this question, I have 2 query
1) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '12/31/2013'
This query give proper data from the table.
But in 2 query
2) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '01/10/2014'
This query not display any date from table, how this not display any data, there is some year change problem in mysql server.
Please help me.
You have to convert string to date for comparing two dates otherwise it consider as string. For that you have to use STR_TO_DATE() function
Try this:
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2013-12-31'
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2014-01-10'