I have a the following query :
select * from table where createddate>='03-Feb-2020' and createddate<'04-Feb-2020'
The above query will give me incremental count for a single day.
How do i generalize the above query so that i can get the entire historical data/full dump without changing the where clause.
For example:
select * from table where createddate>='VARIABLE1' and createddate<'VARIABLE2'
Is there a way that without changing the schema of the sql query i can just pass in different values for the createddate to get the full dump?
Is this what you want?
where createddate >= '1000-01-01' and createddate < '9999-12-31'
Note that dates in MySQL must be formated as YYYY-MM-DD.
Do you want group by?
select date(createddate), count(*)
from table
group by date(createddate);
This returns the count for each day.
Related
Is there any difference in any sql engines (and particularly in mysql) in the following two queries?
SELECT * FROM table where date = '2019-01-01'
And:
SELECT * FROM table where date = DATE('2019-01-01')
Doing an explain returns the same result, but perhaps there's some sort of difference that I'm not catching? I need to run a query against a multi-billion row table and am trying to optimize it before running.
There should not be. The expression DATE('2019-01-01') should be evaluated during the compilation phase turning the result into a date. Similarly, the constant value '2019-01-01' is implicitly converted to a date for the comparison.
This allows MySQL (and most other databases) to use indexes and partitions defined on that column.
Date() function Extracts the date part of a date or date/time expression
for example the value of the field name BirthTime is "2017-09-26 16:44:15.581"
so you have to use the following query to check the date :
SELECT DATE(BirthTime)
result is : 2017-09-26
My query to get data by month
$this->db->select('vehicle_make_and_model');
$this->db->join('tukai_drivers','tukai_drivers.driver_id=reqn_challans.dc_driver_id','left');
$this->db->where('dc_challan_date','MONTH(2)');
$this->db->order_by('dc_challan_id','desc');
$result=$this->db->get('reqn_challans')->result();
I am trying it via codeignitor like this
$this->db->where('dc_challan_date','MONTH(2)');
It did not work? What is wrong
Storing date like this
http://awesomescreenshot.com/0424q72ne5
You need to update your where condition from
$this->db->where('dc_challan_date','MONTH(2)');
to
$this->db->where('MONTH(dc_challan_date)','2');
The actual function is like MONTH(date) so here you have MONTH(dc_challan_date) which is your column name
In SQL databases you should NOT apply functions to data to suit the where clause so that you maximize the benefit of indexes (i.e. get better performance). For example, instead of using MONTH(dc_challan_date) you can achieve getting a month of data using the >= with < such as the following SQL:
select * from table
where dc_challan_date >= '2015-02-01'
and dc_challan_date < '2015-03-01'
The database optimizer can use an index on column dc_challan_date in that query. The example uses "sargable predicates".
I need to calculate the num of rows created on a daily basis for a huge Table in mysql. I'm currently using
select count(1) from table_name group by Date
THe query is taking more 2000sec and counting. I was wondering if there's any optimized query or a way to optimize my query.
If you're only interested in items that were created on those dates, you could calculate the count at end-of-day and store it another table.
That lets you run the COUNT query on a much smaller data set (Use WHERE DATE(NOW()) = Date and drop the GROUP BY)
Then then query the new table when you need the data.
Make sure that "date" field is of "date" type, not datetime nor timestamp
Index that column
If you need it for one day, add a "where" statement. i.e. WHERE date="2013-07-10"
Add an index on the Date column, there's no other way to optimize this query that I can think of.
CREATE INDEX ix_date
ON table_name (Date);
I have the following problem:
In mysql I have a table which contains two date columns start_date and end_date. The date format is yyyy-mm-dd. What I am trying to do is to get all data from all the rows where a specific date, lets say '2012-03-05' mateches one of these date columns or are something in between.
How can I create a good sql-query that gets the data needed? I've checked on the between statement but I don't really know if that's the best way to go. I guess this is generally a simple task but I just can't figure a good query out.
Thanks.
SELECT * FROM table WHERE start_date <= '2012-02-29' AND end_date >= '2012-02-29';
Should do it.
This is a very common way to structure your tables with ranges of dates, especially in temporal database designs. It lets you perform range-based queries very efficiently, assuming that indexes on both columns exist. You query the data like this:
select *
from mytable t
where t.start_date <= #desired_date and t.endDate > #desired_date
#desired_date is the date for which you would like to query, e.g. '2012-03-05'.
Note the <= on one side and > on the other side, without =. This is done to ensure that the from-to ranges define non-overlapping intervals.
Not sure, try something like this:
SELECT
*
FROM
mytable
WHERE
'2012-03-05' BETWEEN start_date AND end_date
SELECT * FROM mytable
WHERE '2012-03-05' BETWEEN start_date AND end_date;
I am trying to "SELECT" a time range while ignoring the date portion of a "TIMESTAMP" field (09-09-11 10:23:03).
The query below works but has huge performance costs...
SELECT * FROM Table WHERE Time LIKE '10:09:13'
What is the fastest way to do this on a table with 1/2-1 million rows without using the "LIKE" command?
SELECT * FROM Table WHERE TIME(Time) = '10:09:13'
That should do it.