Change order of items coming from MySQL in PHP - php

I need to pull a list of items from a MySQL database and then display them using PHP. I have this figured out. My question is how to change the order of the items. I know you can order them based on a certain field from the database, such as 'id' or similar.
What I need to do is do a calculation on 2 of the fields and then order the resulting list based on that calculation. For example, I need to show an Integer-X from the db and Integer-Y. I also need to show the result of Integer-X minus Integer-Y and I need to sort the list of responses based on that result of Integer-X minus Integer-Y.
Any help would be much appreciated. Thanks in advance.

You can put calculations in the ORDER BY clause:
ORDER BY integerXField - integerYField
Also, if you give an alias to the calculation in the SELECT list, you can sort by the alias:
SELECT
... integerXField - integerYField as theDiff
..
ORDER BY theDiff

Related

Get Sub total, total and grand total report in php from mysql data

I need to find out :
1) How many total entries have done by a particular user on a date
2) How many total entries have been done on a particular date.
I am able to run this query :
SELECT app_user,DATE(app_doe),COUNT(*) AS entries,`institution`,`qualification` FROM `details` WHERE MONTH(app_doe)="12" AND YEAR(app_doe)="2017" GROUP BY app_user,DATE(app_doe),`institution`,`qualification` ORDER BY DATE(app_doe) DESC,app_user
and get the following output from the mysql table.
I need to arrange data in a report based on the above mysql query output like this using php , but i am not getting any idea how to count total,subtotals as mentioned above :
Please provide pointers on how to go ahead ...
Start by changing GROUP BY whatever to GROUP BY whatever WITH ROLLUP in your query. You'll get extra rows at the end of every grouping with rolled up values, and NULL values indicating the groups over which each rolled up value was determined. https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html
You may need to adjust your rendering program to do a good job presenting the modified query.

Mongodb distinct records from last record not from first

In mongodb php, I need to get a distinct list of records based on one field but the search should start from the last record.
db.collection.distinct doesn't sort records and doesnt work with a paginated data set, and it's not given the way it searches for distinct values: any order you see is incidental and could change with implementation or access plan.

Generate Custom Order Number

I've tried to generate custom order number. My MySQL table is like:
tblOrder
id int(11) AI; ordno varchar(50);
I try to use following function inside php block as before record added event to accomplish this.
{
$sql="select max(substr(ordno,9)) as mx from ord where substr(ordno,7,2)=month(now()) order by mx";
$rs=CustomQuery($sql);
$data=db_fetch_array($rs);
$str="OR-";
$str2=date("Ymd");
$str3=($data["mx"]+1);
$values["ordno"]="$str$str2".str_pad($str3, 5, 0, STR_PAD_LEFT);}
Problem is when I run this code it always give me same order number like OR-2014013000001 for all record entry. It does not change. Could you please help me out? Thanks in advance.
I think the real issue is being hidden by the way you're parsing the composite ordno column. Remember that SUBSTRING is 1-based, not zero-based indexing.
$sql="select max(substring(ordno,12, 5)) as mx from ord where
substring(ordno, 8, 2)=month(now()) order by mx";
...seems to be the correct parsing to get the values you want.
It really would be better to store the order data in separate fields instead of encoding meaning into a single column. You may be stuck with it this way, but if you CAN edit it, why not compute the order number on the fly by grabbing a well structured date order date column and an int order sequence column?

Retrieve the mean of results from imputed data PHP/MySQL

First of, I'm pretty new to this site and coding in general so please explain in simple terms as I'm still learning! Thanks
Ok, so I've got a database of results. These are 1-6 ratings. I've already created the ability to retrieve certain results (user, group, all).
But now I'm wanting to alongside retrieving the group and all results to display at the top of the results a mean for each question.
So to start I'm wanting something like this I believe.
SELECT sum(r1), sum(r2), sum(r3) so on,
FROM table
This is where I get confused.
I think I'd need a variable to contain these and then another that counts the amount of entries to divide the total of r1 hence the mean.
Any ideas?..
To calculate a mean, use the AVG function, e.g.
SELECT AVG(r1), AVG(r2)
FROM table
See the MySQL docs.

Reordering MySQL

I have probably a very simple question.
I have a MySQL database called "alldata", which contains various variables. The first column called LogDateTime contains date and time. Now the thing is that I want the db to be sorted from the oldest to the newest - in other words by column 1.
I know how to do a MySQL query using ORDER BY Logdatetime etc. But what I would like to do is to reorder tha data in the actual database and save it ordered. Right now there are some dates a bit messed up. Then I would not have to use the ORDER BY statement in all my queries because the database would already be sorted.
Could anyone please just give me the SQL command I should use to reorder the entire database?
You cannot setup a relational database table to return results ordered by a specific column of your choosing. You need to use ORDER BY. You could work around this by using views.
The view definition would include an ORDER BY. You could select from the view and it would show results in your desired order.
CREATE OR REPLACE VIEW `mytable_view` AS SELECT * FROM `my_table` ORDER BY `my_date_column`
Then you can select the data:
SELECT * FROM `mytable_view`
Results are shown in desired order.

Categories