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;
Related
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.
I have a table as shown here:
In the table there are two columns named DateFrom and DateTo.
I want to fetch row data from or between these two dates. e.g. If I want to fetch data from 2018-12-27 to 2019-01-10 it should return the two rows with HPID 1 and 6 - as both row have or comes under the mentioned dates.
Now I don't know what the SQL query should be. I have tried BETWEEN but with no result.
Actually I am working on a small hotel management system and the table shows the cost of any hotel between certain dates. So when the user searches for hotels for date between two dates it should show costs which overlap those dates.
DateFrom and DateTo Are not datetime.
Stefan Taseki's answer is almost right but doesn't deal with overlaps correctly. Swap the start and end dates round in that query and it should do the job:
SELECT *
FROM [YourTable]
WHERE DateFrom <= '2019-01-10' AND DateTo >= '2018-12-27'
P.S. You should always store your dates in datetime columns. Dates are not text. You will have issues with a) sorting, b) comparisons, and c) presenting dates in different formats if you don't do this. The datetime data type exists for good reasons, you should use it. Consider changing your database now while you can.
P.P.S. Your data is also de-normalised. Country, City, HotelName, HotelCode and HotelStar should all be in a separate "hotel" table (and country and city each should be in different tables too), and then with a foreign key on Hotel ID only in this costs table. Otherwise you keep repeating data which should only be entered once. I suggest you learn about relational database design and normalisation, if you didn't realise this.
This should work:
SELECT *
FROM [YourTable]
WHERE DateFrom >= '2018-12-27' AND DateTo<= 2019-01-10
First of all make sure that your dates are in single quotes
'SELECT * FROM your_table WHERE DateFrom BETWEEN '2018-12-27' and '2019-01-10' OR DateTo BETWEEN '2018-12-27' and '2019-01-10';
Something that is not clear is what are you trying to achieve with this query.
Casting may be required, use below query
select *from your_table_name
where date(DateFrom) >= date('2018-11-27') AND date(DateTo) <= date('2019-01-10')
I was hoping if any of you could be of any help, if I have a huge database with million of entries and all I need is information relating to a specific year (say, 2015), if I use the following query, performance wise will this be best practice or is there another way to have a better query,
CREATE INDEX table1_idx ON table1 (date_column);
SELECT text, date_column
FROM table1
WHERE datepart(yyyy, date_column) = 2015;
Any help relating to this will be highly appreciated.
If this is about SQL Server, that is not the way to do it. Using any functions on the fields makes in non-SARGable.
Always use correct search criteria like this:
SELECT text, date_column
FROM table1
WHERE date_column >= '20150101' and date_column < '20160101'
You might also want to consider adding text as included column to prevent key lookups:
CREATE INDEX table1_idx ON table1 (date_column) include (text)
MySQL can't use an index range scan operation, due to the function wrapped around the column. The function has to be evaluated for every row in the table.
To get an index range scan operation, do comparisons on the bare column.
For example:
SELECT t.text
, t.date_column
FROM table1 t
WHERE t.date_column >= '2015-01-01'
AND t.date_column < '2015-01-01' + INTERVAL 1 YEAR
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".
So, the query I'm trying to make looks like this right now:
SELECT `jid`, MAX(`start`) as `start` FROM `dates` WHERE `start` < NOW() GROUP BY `jid`
The table dates basically holds a DATETIME value start associated with a job.
As you probably guessed, jid is the id of a job, stored in another table.
A job can have many dates
What I'm trying to accomplish is to find out what jobs have all their start dates in the past.
My thought was to select the highest values of the start dates, grouped by jid and if they were in the past, it would imply that all other dates associated with the same job are also in the past.
This query isn't working because it's checking for start dates in the past and then selecting the highest of those. This doesn't exclude the possibility of another date for the same job, lying in the future.
I have no idea how I could proceed now. Any help or clue is appreciated.
Cheers - Tom
You have to use HAVING :
SELECT jid, MAX(start) as start
FROM dates
GROUP BY jid
HAVING MAX(start) < NOW();
HAVING acts a bit like WHERE. It filters out the rows after they were selected. Usually (and actually, I can't think of any other case), you only use HAVING with aggregate functions.
(I hope you really inserted dates in the future in your table, though!)