I'm making a restaurant web manager.
I need to get the total of all the months of a year.
Do I need to make a query for each month, or there is a way to make it all in one query with a FOR (loop).
Each month has different orders with different values which has to been summed to the month to show them separatedly.
I can't get a clue of how to do it if it's not with each month one by one.
I think this way may get it.
for ($sum=0; $sum < 12; $sum++) {
$result = mysqli_query($mysqli,"SELECT table_number, SUM(price) as totalprice FROM orders WHERE MONTH(dates) = '$sum' && YEAR(dates) = '$year' GROUP BY table_number");
}
I have a mysql database.
The orders are placed on a table "orders" whith following structure:
.id(int)
.name(varchar)
.price(int)
.table_number(int)
.date (date / Y.m.d)
I assume you have a MySQL database. And I assume you have one table where all orders are stored. I assume you have a field value (double) and a field time (timestamp). Try
SELECT
YEAR(`time`), MONTH(`time`), SUM(`value`)
FROM `table`
GROUP BY YEAR(`time`), MONTH(`time`)
ORDER BY YEAR(`time`) ASC, MONTH(`time`) ASC;
Also check the MySQL manual:
https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
Make a table of whatever you want to sum. Add a column in it of the data you want to sum for each month and add another column in the table which stores the month every row belongs to.
Now query this table with php to get all the rows of the table and make an arrray of all the months, check which month every row belongs to and add the data you want to sum to that specific months total summation.
Like this, you can easily find the sum for each month and can achieve what you are trying to do.
Related
What I'm trying to do.
To pull data from a database. With one column 'serial_no' to only pull 1 of each value so it's unique, and any other values within the 'serial_no" column to not show if another one of the same value exists. So in column 'serial_no' there could be 35k values but it would only show 35 if there are a total of 35 unique serial numbers. Once I have them I need them to show the latest first by 'datetime' column.
Current outcome.
I have the data pulling through, and it's only showing once of each 'serial_no' however, it's not showing the latest first, like it seems to be ignoring the ordering or just pulling through the first one it sees rather than the latest.
These 2 PHP queries I have used and working but not 100% how it should. The first one, i only want 'serial_no" distinct not all columns, so maybe that's why this one is not working.
$sql = "SELECT DISTINCT serial_no, datetime FROM wp_clicker_data ORDER BY datetime DESC";
The other one which works fine apart from it does not show the latest value of a specific serial_no
$sql = "SELECT * FROM wp_clicker_data GROUP BY serial_no ORDER BY datetime DESC";
Any ideas how each unique value of column 'serial_no' can pull through the latest entry based on the latest 'datetime' column?
Thanks!
Use Max and GROUP BY to get your desired output as below-
SELECT serial_no,
MAX(datetime)
FROM wp_clicker_data
GROUP BY serial_no
If you want the latest row for each serial number, then use filtering:
select cd.*
from wp_clicker_data cd
where cd.datetime = (SELECT MAX(cd2.datetime)
FROM wp_clicker_data cd2
WHERE cd2.serial_no = cd.serial_no
);
GROUP BY is not appropriate when you want to retrieve entire rows. Using SELECT * with GROUP BY doesn't make sense, because there are columns in the SELECT that are not in the GROUP BY. And this construct generally won't work (with the default settings) in the more recent versions of MySQL.
I am trying to put together a query so that when I add a record, the database assigns it a sequential number based on how many records there are in the next year, then adding the next number, e.g.
2016/1
2016/2
This is the query I have so far:
CustomQuery("Select Count(*) as count1 from ApplicationRegister where ClientFk='".$values['ClientFk']."' AND ???
What I want is to use the 2nd logic operator to say YEAR(now)=Year(ApplicationDate), where ApplicationDate is a date field within the database.
How do do this?
Hi I have a database with a column containing email addresses and a second column containing category and a third column containing date.
What I want to do is count the number of unique email addresses in category 'A' between multiple date ranges. So I have this:
SELECT COUNT(DISTINCT email) as counter
FROM table
WHERE category = "A" AND date < "2015-12" AND date > "2015-11";
Then I'll do a separate query for the second date range.
NOW HERE'S MY PROBLEM:
If an email address appears in month one, and also in month two it will go on the count for both months because it's unique within the range I'm querying.
How do I create a query that will count the unique email addresses for a year let's say, then count the distinct entries in a month period without including the duplicates?
Thanks!
If you want custom date you can set the value to vars
$custom_date_begin = "2015-01-10";
$custom_date_end = "2015-02-10";
then for vars and the month group by you can use somethings like this
"SELECT COUNT(DISTINCT email) as counter
FROM table
WHERE category = 'A'
AND date >= '$custom_begin_date' AND date <='$custom_end_date'
group by MONTH (date);"
Information provided is not enough for writing adequate query. So I'll try to guess details.
Lets assume that we need to count number of unique emails for each month and category.
The query could be like the following:
SELECT dt, category, COUNT(*) AS cnt
FROM (
SELECT LEFT(`date`,7) AS dt, category, email
FROM table
GROUP BY LEFT(`date`,7), category, email
) x
GROUP BY dt, category
If you have variable date ranges, then you'd better group on daily basis, and then count emails via script for each date range.
First sorry for the long question title.
My question/situation is as such.
1.) I have 2 tables in mysql
2.) In first table, each listing has a unique id(each listing is in 1 row)
3.) In the second table it has the name/tags for images linked to the listing id,from the first table
4.) Each listing can have multiple images(multiple row in the second table).
What i am trying to do is to pull all the listings from table 1 and then use the listing.id from table one to pull all the rows of images from table 2 that are linked to the listing.id.
I am confused at the moment because there are multiple rows that has the same listing.id from table 2. ANd i tried query to display* but it only echo the last image(row) from table 2.
It doesnt seem to work when i join the 2 tables. And i am not sure if i query it twice then push array together.
Thanks for your time
$result=mysqli_query($con,"SELECT * FROM Listing JOIN listingpic ON
(Listing.id = listingpic.listingid)
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY GROUP BY Listing.id ORDER BY Listing.id DESC") or die( mysqli_error($con));
while ($row = mysqli_fetch_assoc($result))
{
$output[] = $row;
}
if (!empty($output)){
echo json_encode( $output );}
else{
echo json_encode( [] );
}
You only get one result per id, because you used GROUP BY Listing.id. If you do not group them, you get one result row for each table 2 row, including the Listing id each time.
If you wish to retrieve the data in one query in the form "one id: multiple data", you can use GROUP_CONCAT for example and then explode() the retrieved string result.
Otherwise get all ids from table 1 and then iterate over them in PHP and do one additional query per ID
Pro tip: Don't use the viciously confusing MySQL extension to GROUP BY. Read this: http://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html
Pro tip: Don't use SELECT *, especially when you're joining tables. Instead, enumerate the columns you want in your result set.
Inherent to SQL is the idea that resultsets, like tables, are rectangular. They have rows and columns. Each row usually represents some real world item -- an "entity" -- and each column represents some attribute of that entity.
The result set you describe will, inherently, repeat information from your first table so it can show the info from the second table row by row.
What you want for a query is this, I think.
SELECT Listing.id, listing.date,
listingpic.id, listingpic.url, listingpic.caption
FROM Listing
JOIN listingpic ON Listing.id = listingpic.listingid
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY
ORDER BY Listing.id DESC, listingpic.id
This will give you one row per image.
If you're running out of memory it's because your result set is massive. You may want to limit it somehow, either using a first and last publication date:
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())+293 DAY
or with a LIMIT clause.
ORDER BY Listing.id DESC, listingpic.id
LIMIT 100
I have a table of customers with a 1 recorded against their customerid on different dates.
I would like to find the sum of the 1's recorded in descending order. I'm using MySQL and php
Thanks
My guess is that you want the sum of records marked with 1 per customer and sort that result in descending order? If so, the following should do the trick :
select cust.id, sum(cone.one) as number_ones
from customers as cust
inner join customer_ones as cone on cone.id=cust.id
group by cust.id
order by number_ones desc
This is assuming that 'one' is the column containing ones (and only contains 0 or 1 - otherwise you will have to add WHERE cone.one = 1), customers is your customer table and customer_ones is the table containing your customer data.
As i get you right, this is simple sql request what u need:
SELECT COUNT(id) as total from customers
Just make in php:
$sql="SELECT COUNT(id) from customers";
$query=mysql_query($sql) or die(mysql_error());
$res=mysql_fetch_assoc($query);
$summ=$res['total']; //<- Your summ (i.e. quantity of rows in table)
Btw, you can use mysql_num_rows instead.
Or explain please more accurately what output you need. To make sorting by date or any other dependency you will need to make other request using WHERE clause and date comparison.