i need to retrieve multiple selects from mysql table, my need is to simplify the statements
these are my statements
$stardate=dateselect
$enddate=dateselect
SELECT count(distinct(customer)) as customer FROM my_table where date_field BETWEEN'$startdate' AND '$enddate'
SELECT count(distinct(customer)) as computeris1 FROM my_table where computer=1 and date_field BETWEEN'$startdate' AND '$enddate'
SELECT count(distinct(customer)) as computeris2 FROM my_table where computer=2 and date_field BETWEEN'$startdate' AND '$enddate'
I have about 10 fields, each one has 4 or 5 values (choices) want to count the distinct customer for each field (e.g. computer) and all between 2 dates..
another problem is the date is a timestamp of mysql, doesn't return a correct and exclude today's records unless i select the end date as tomorrow then it works, i think it is a time issue, how i can make it count date correctly discarding the time?
You can fix the date/time problem by using date(). As for the rest, you can use conditional aggregation to do everything in one query:
SELECT count(distinct(customer)) as customer,
count(distinct case when computer = 1 then customer end) as computer1,
count(distinct case when computer = 2 then customer end) as computer2
FROM my_table
where date(date_field) BETWEEN '$startdate' AND '$enddate';
This puts the values as separate columns rather than separate rows.
Related
hello everyone for my erp system i want some specific type of data fetch from database
for example if i want number of row added in current month purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE());
but it gets false results
and if i want sum of result in current month then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE())
and if i want today date number of row or purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
so as if i want today sum of purchase then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
my table created on field datetime like 2016-09-01 11:56:45
so please suggest me better query to fetch exact data
thanks
It's not really clear what - if any - the problem is with the last 3 queries, but the first one will get you the data from this month this year and this month last year, the year before that, etc. etc.
So what you need is something like (there are several ways to write that...):
select count(receive_no) FROM tbl where
contact_id=".$contactid."
AND MONTH(created_on) = MONTH(CURDATE())
AND YEAR(created_on) = YEAR(CURDATE())
I'm using mysql and I'm having trouble thinking of a query to count the number of users/visitors for a certain date range. The way that I'm currently doing it is using php, I select the date range and process the data in a for loop and then just count them there. It's actually pretty easy, but the problem is that this method does not work for bigger data of a few million rows. The alternative is to count the distinct values using mysql only and just return a count and not actual data by utilizing the index on the timestamp column. Also, converting the column to a datetime is not an option. Any ideas how I can achieve this?
Here's a sample result set of what I need:
date | count
5-01-13 14
5-02-13 44
5-03-13 23
5-04-13 13
My problem is that I don't know how to group the timestamp column by day.
That should do the trick:
SELECT DATE('datetimecolumn'), COUNT(*)
FROM Table
GROUP BY DATE('datetimecolumn')
You just have to do the same, but instead add a group by clause:
SELECT myDate, count(distinct myField) as cnt
FROM myTable
WHERE myDate BETWEEN ? and ?
GROUP by myDate;
Where the "?" are the dates you use in your original query.
I have a database that contains three different columns: domain, date, length (in that order). What I'm trying to do is count the occurrence of every unique date in the database without knowing what the end date is in the database. The start date is always todays date.
So far I've only been able to count the occurences of a specific date, which requires me to put in an exact date.
What I want the PHP script to do is to start with todays date and output the number of times the date is mentioned (or the number of rows with that date) and then continue until it reaches a date that doesn't have a value (you could call this the end date).
I'm surprised and frustrated that I haven't been able to find the solution yet. It seems like a very easy thing to do.
I'd be super happy for any hints, tips or solutions.
Use a combination of a GROUP and WHERE clause.
SELECT COUNT(*) AS `occurrences`, `date` FROM `table` WHERE `date` >= CURDATE() GROUP BY `date`
Use group by and order by
SELECT dateCol, Count(*) FROM myTable
WHERE dateCol >= date(now())
GROUP BY dateCol
ORDER BY dateCol ASC
Edit: SQLFiddle with your example: http://www.sqlfiddle.com/#!2/5e86b/2
Trying to get my head around, If I select a record such as WHERE item_id='$item_id' AND date(datetime)='2012-06-25' and if that record does not exist so I want to get the nearest-latest record after that date. How can I achieve that in a query?
All I can think of the only way right now is if num_of_rows is 0 then I add 3 days period ahead to that day and search again and get the DESC datetime LIMIT 1 (in case there are multiple rows). But who knows I can do it with just a query.
The record could have multiple rows in one day. So if a particular date has no record, how to get the next nearest available data given the same $item_id?
SELECT *
FROM table
WHERE field <= '2012-06-25'
ORDER BY field DESC
LIMIT 1
I think this is what you are looking for:
SELECT *
FROM my_table
WHERE datetime BETWEEN '2012-06-25 00:00:00' AND
DATE_ADD('2012-06-25 00:00:00', INTERVAL 3 DAY)
ORDER BY datetime ASC
LIMIT 1;
also create index on field datetime for faster performance.
This will bring back the item closest to the date that you enter into the query. It won't however look for before or after, just find the closest date to what you enter in.
select
min(abs(DATEDIFF(date(datetime),'2012-06-25'))) as minDiff
,yourID
from table1
group by yourID
order by 1 asc;
I have 2 tables:
The first contains data for the current day (per hour) - this table has raw data, that needs to be processed.
The second contains data for previous day (per day) - this table already has the values calculated for every day.
I want to combine this to rows in a MySQL query, so that I return the data for previous dates (per day) and for current day (again, per day).
Currently I am using 2 mysql queries:
//table 1, data for current day
$qry1="
select DATE_FORMAT(completedate,'%Y-%m-%d') as date, SUM(IF(complete = 1,affpayout,0)) as pay, COUNT(*) as leads
from leads
where affiliateid={$_SESSION["id"]} AND completedate>'$date'
GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d')";
//table 2, data for previous days, already processsed, we just need to select it
$qry2="
select DATE(date) as date, affrevenue as pay, totalleads as leads
from leadsdays
GROUP BY DATE(date)";
How can I combine the 2 efficiently (speed performance is an issue)? What would be the best way to do this?
Try to UNION. Also, make sure your tables are indexed properly. Looks like you'll want completedate and affiliateid indexed at least. Not sure how much more efficient two queries is versus one union though...
$qry = "(select
DATE_FORMAT(completedate,'%Y-%m-%d') as `date`,
SUM(IF(complete = 1,affpayout,0)) as pay,
COUNT(*) as leads
from leads
where affiliateid={$_SESSION["id"]} AND completedate>'$date'
GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d'))
UNION
(select
DATE(`date`) as `date`,
affrevenue as pay,
totalleads as leads
from leadsdays
GROUP BY DATE(`date`))
ORDER BY DATE(`date`)";