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
Related
Thank all of you for your help. My Access database is coming along nicely.
My new question is:
I have three fields that are to be considered to get the result I am looking for.
Date, EMPID and the RESULTS field.
The date is simply a date with no time.
The EMPID is a unique employee identifier.
The Results field states either pass or fail.
What I am trying to do is on any single date (there may be many dates, but each is to be considered separately) an employee may test many times and have multiple failures (there can only ever be one passing result). If on the same date the same employee passes, then all fails are to be removed. If there is no pass, then leave one fail.
Thank You,
Query 1: QueryPass
SELECT *
FROM Table1
WHERE Results="Pass";
Query 2: QueryFail
SELECT *
FROM Table1
WHERE EmpID & ResDate Not In (SELECT EmpID & ResDate FROM QueryPass);
Query 3: QueryReport
SELECT EmpID, ResDate, Results FROM QueryFail
UNION SELECT EmpID, ResDate, Results FROM QueryPass;
NOTE: Date is a reserved word in Access, should avoid using reserved words as names for anything.
Since there can be multiple rows per person (and all in the same table), and if someone passes, it will be the 'last' record for that person, the following will give you what you need.
SELECT Last(Table1.[TestDate]) AS LastOfTestDate,
Table1.[EMPID], Last(Table1.[TestResult]) AS LastOfTestResult
FROM Table1
GROUP BY Table1.[EMPID];
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.
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.
SO I have to list the names of anyone in the tenant family who is older than the tenant itself. There are two tables. Tenant and tenant_family. I have tried comparing the two dates to see which DOB in the tenant_family table is less than (meaning they would be older) the DOB in the tenant table.
This is what I have so far but it appears to be wrong. Can someone direct me towards a way of getting the right output?
SELECT DISTINCT tenant_family.name
FROM TENANT_FAMILY, tenant
WHERE tenant_family.dob < tenant.TENANT_DOB;
Use the DATE function to extract the date part of your query for comparison like:
select distinct tenant_family.name from TENANT_FAMILY, tenant where DATE(tenant_family.dob) < DATE(tenant.TENANT_DOB);
In case your dob and TENANT_DOB are not stored as the DATE data type in your database or they are stored as DATETIME then you might extract only the date part using the function:
select distinct tenant_family.name from TENANT_FAMILY, tenant where date_format(tenant_family.dob,"%y-%m-%d") < date_format(tenant.TENANT_DOB,"%y-%m-%d);
This should give you the correct results after comparison.