My table name is client_details and my date field contain group of dates like
03/03/2015,04/13/2015,05/11/2015,06/08/2015,09/04/...
03/18/2015,04/28/2015,05/26/2015,06/23/2015,09/19/...
03/20/2015,04/30/2015,05/28/2015,06/25/2015,09/21/...
03/26/2015,05/06/2015,06/03/2015,07/01/2015,09/27/...
03/26/2015,05/06/2015,06/03/2015,07/01/2015,09/27/...
03/06/2015,04/16/2015,05/14/2015,06/11/2015,09/07/...
03/13/2015,04/23/2015,05/21/2015,06/18/2015,09/14/...
04/16/2015,05/27/2015,06/24/2015,07/22/2015,10/18/...
03/03/2015,04/13/2015,05/11/2015,06/08/2015,09/04/...
03/04/2015,04/14/2015,05/12/2015,06/09/2015,09/05/...
03/19/2015,04/29/2015,05/27/2015,06/24/2015,09/20/...
I want to search a date between '03/26/2015' and '05/12/2015'.How can i write the query?
You may try this:
SELECT * FROM client_details WHERE `DateField` BETWEEN '03/26/2015' AND '05/12/2015';
SQL Fiddle
EDIT:-
So your comments show that you are storing the dates by seperating them with comma. I would seriosuly discourage that practise and recommend you to change the design of your table. That is a poor way of storing the dates.
Reasons:
It will lead you to troubles everytime you want to use date functions.
It is not performance effective.
Alternate Solutions:
Change the design of your table.
Create a seperate table which stores the dates corresponding to a user_id and then join this table with your client_details table using the user_id and get the date which you want.
Related
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 have a database "warehouse" including tables of daily inventory records, one table for each day.
Now, I need to check the historic change of the inventory level. The output will print the inventory of each day given certain criteria.
I am not sure how to describe it, so I created a simplified sample of the schema, its tables and the expected output.
The schema "warehouse" has a list of tables:
Each table contains the same columns for product ID and inventory, below is table 101
For each table, I need to do a query:
select count(*) as num_of_product_with_inventory from [table name]. After I have the query result from each table, I should have an output like in below:
Can anyone show me how the query should look like to get the final output? I only know the basic queries and have no clue how to put these together. Thank you!
The data model you have is making your work harder than it should be.
If you must keep it, you will need to use a stored procedure or do the loop in your code (not in sql).
But you should really do is change the data model.
It is not recommended at all to create a table per day!
It's a mix of DATA with METADATA. The table structure should represent different types of data that you store, while the fact that you had different inventory on date X vs date Y should be in your data.
So, recommend to create one table with columns date, product_id and warehouse_inventory. If it gets too big, you can partition it by date (week/month/..). Then you can easily get your data with something like:
SELECT date, count(*) AS num_of_products_with_inventory
FROM daily_inventory i
WHERE i.date BETWEEN '<some date>' and '<some date>'
GROUP BY date
My mysql database table has multiple entries with the following structure:
id, title, date, time
There are presently 30 entries in the table and some of those share a common date.
What I'm trying to accomplish is retrieving the database data in such a way that will group them under common dates. So, all entries that share the same date will be grouped in an array indexed by that common date.
In another post, I learnt INDEX BY is great for what I'm trying to achieve but it works only/best on unique fields.
So, I am just curious if there is anything else that could help efficiently group my database entrie.
SELECT date, GROUP_CONCAT(title)
FROM tbl
GROUP BY date
ORDER BY date;
Don't worry about performance until you have thousands of rows.
I have a table lead and there is a field called added_on (datatype timestamp), I want to to fetch only the leads which are interested in a particular product and the reports should come monthly.
interested_in is a field in the lead table where the interested product's id will be stored as a comma separated values.
and $prod_id is stored with a product id which has to be checked.
the below query works fine just to fetch out the leads which are interested in a particular product. but i want the results to come month by month.
select*from lead where find_in_set('$prod_id',interested_in)
Please guide me what i have to do to achieve that
TRY
WHERE MONTH(added_on) = $giveMonthNumber
OR
WHERE MONTHNAME(added_on) = $givenMonthName;
Reference :
MySQL date time functions
Do this:
select * from lead where find_in_set('$prod_id',interested_in) group by added_on
I have a bunch of data ordered by date and each table holds only one month of data. (The reason for this is to cut down query time, I'm talking about millions of rows in each month table)
For ex.
data_01_2010 holds data from 2010-01-01 to 2010-01-31
data_02_2010 holds data from 2010-02-01 to 2010-02-28
Sometimes I have to query these tables according to a specific date range. Now if the range is across multiple months for ex. 2010-01-01 to 2010-02-28 then I need to query both tables.
Can this be achieved with a single query?
Like for example:
SELECT *
FROM data_01_2010, data_02_2010
WHERE date BETWEEN '2010-01-01' AND '2010-02-28'
The problem with the above query is that it says the column date is ambiguous which it is, because the column is present in both table. (tables have the same structure)
So is this achievable with a single query or do I have to query it for each table separately?
This is a perfect example of why partitioning is so powerful. Partitioning allows you to logically store all of your records in the same table without sacrificing query performance.
In this example, you would have one table called data (hopefully you would name it better than this) and range partition it based on the value of the date column (again hopefully you would name this column better). This means that you could meet your requirement by a simple select:
SELECT * FROM data WHERE date BETWEEN '2010-01-01' AND '2010-02-28';
Under the covers, the database will only access the partitions required based on the where clause.
Reference:
http://dev.mysql.com/doc/refman/5.5/en/partitioning.html
If you do the logic elsewhere to figure out what tables you need to query, and each month has the same schema, you could just union the tables:
SELECT *
FROM data_01_2010
WHERE date BETWEEN '2010-01-01' AND '2010-02-28'
UNION ALL
SELECT *
FROM data_02_2010
WHERE date BETWEEN '2010-01-01' AND '2010-02-28'
But if you need the query to calculate which tables to union, you are venturing into realm of stored procedures.