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?
Related
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.
I want to create a statistics page where i can count the number of reasons given in a table ...The reasons are created dynamically and stored in another table.
Table1 contains all the reasons which populate a drop down box...
Table2 contains the entries given by the user one of the columns is the reason value.
I would like to count the amount of times each reason appears .
Thankyou for your time and help
Looks like simply group statement:
select
reason.name reason,
count(*) count
from entry, reason
where
entry.reason = reason.id and
entry.time > now() - interval 7 day -- you probably need some filter
group by reason.id
I have a table in that in one column I have a list of userid's separated by commas, and another column with date, this action is repeated for every day. How can I get month-wise data of who attends the class based on user id.
I need data like month-wise.
You need to separate userId else that will make very complicated sql and slow your server down. One record for date to userid. Then it is very simple;
select count(id) from `table` GROUP BY YEAR(record_date), MONTH(record_date)
I am writing a complex MySQL query. My actual query is more complex than I mentioned below.
I have a table named example and columns are id, name, option_1, option_2 . Of course id column is PK . I want to retrieve like this:
SELECT `id`,`name`,count(`option_1`),count(`option_2`)
My problem is I want to use "GROUP BY `id`" for count(`option_1`) and "GROUP BY `name`" for count(`option_2`) respectively. Now I have to break down it into multiple code in my php code.
How can I achieve what I want in a single query?
What you're asking for doesn't make a ton of sense. You want option 1 grouped by id and option 2 grouped by name, and you want to show all four columns in one result set.
First of all, if id is a primary key, then the count will just be the number of rows in the table since there will be no duplicate ids.
Second, even if id wasn't a primary key, and there were duplicate values, the grouping is different, so the counts represented in your result set would be grouped incorrectly.
Given that id is a primary key, perhaps your intention is actually to get a total count of the rows in the table. Perhaps this query would suit your needs?
SELECT
name,
COUNT(option_2) AS options
FROM
example
GROUP BY
name
UNION ALL
SELECT
'Total' AS name,
COUNT(*) AS options
FROM
example
This should get you a count of all the option_2 values, grouped by name, with the final row having a name of 'Total' and the count being the total number of rows in the table.
Aside from that, I'm not sure you're going to find the answer you're looking for due to the problems you would encounter matching up groupings.
here's the scenario. I am generating a report of all the members who have dues to pay for a certain time period.
I am successfully selecting and displaying each database entry as a row in a html table.
The problem is the total fields the report must have. Each member pays different amounts based on what services they use, so I must add the values in each field individually to ensure proper result.
Question is, how do I go about adding the rows/field together?
Edit:
To clarify. I am adding dues paid and donations paid fields. They are classified and integer in mysql database.
Example, let's say that my query returns 3 results. I wish to add the dues paid for all 3 results and display it as total_dues_paid. Same idea for donations.
This must be dynamic for any given number of results, as this changes month to month and we have seen several hundred results in some months.
Thanks
To add fields (columns):
SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM table;
To add rows together, use the SUM() aggregate:
SELECT
userid,
SUM(col1) AS col1_total,
SUM(col2) AS col2_total
FROM table
GROUP BY userid
You can add in your query string.
SELECT (field1 + field2) AS Total
FROM table