reorder results of mysql query php - php

I run a mysql query that gets specific results from a table. I then want to print these results in two html tables. The first ordered by one column which is already done by putting ORDER BY into mysql query. But then I want to print the results ordered by a different column. However, I don't want to run a mysql query again as this is too slow.
So to sum up:
Is there a way to reorder the results of a mysql query?
(sorry if the question is unclear, it is my first time using the site.)

PHP has some really great sorting functions. I would most likely make a user-defined sorting function and use that with your result set:
http://php.net/manual/en/function.usort.php
Sorting with PHP is generally very fast. Faster than a 2nd query if your result set is not too large.

You can simply reorder the resultset yourself with one of the sorting methods in php.
http://php.net/manual/en/array.sorting.php
I assume you would want to use usort() here: http://php.net/manual/en/function.usort.php

Why not show it just once and use the jQuery Table sort plugin. Also, if you want to show it twice, there is no problem in making the table sortable.
The advantage would be performance increases as you are transferring the sort operation from server side (PHP) to client side (Javascript).

If you really want to use the sorting features in MySQL, as opposed to those in PHP, one option is to write your PHP code to query twice. In your first query:
SELECT id FROM table_name WHERE (whatever) ORDER BY first_criteria
Then in the second query, fetch rows:
WHERE id IN (2,17,388,etc.)
ORDER BY different_column
. . . passing the IDs from your first query into the IN clause, of course.
Not sure whether this would be faster or slower than sorting in PHP. Might be worth testing.

Related

PDO MySQL vs PHP Usort

I get my array of data via MySQL PDO:
"select * from table"
Is it faster to order using function within query such as
"select * from table order by key"
Do I sacrifice a lot of efficiency by using a PHP usort or any other array sort compared to a straight mysql order query?
$prepare=$database->prepare("select * from table");
$prepare->execute();
$fetch=$prepare->fetchall(PDO::FETCH_ASSOC);
usort($fetch, ...)
In general mysql order works very fast and it handy to use it, but it's correct in case where you have indexes and you use that index in query for ordering data and explain of that query is good, but in some cases php sorts works really faster (i have situation where my query selects huge batch of records that group it and than having it and than order a small result by field without index).
There is no one rule for this, you should benchmark both approaches.
The problem here is with your toy example. In reality you never run a query like "select * from table". While with real live queries you never select the whole table, but just several records. So there will be just no way to sort records in PHP, and you will have to stick with sorting on mysql side, for which purpose a database were invented.

Filter data from MySQL using WHERE or PHP?

In terms of performance, what's better between select all rows from a table and then filter the results with PHP, or filter the query directly using WHERE (multiple conditions) ?
Using where condition is the best choice because this query will run faster that the first one.
Indexes on the fields that appear in WHERE or GROUP BY or ORDER BY clauses are most of the time useful.
Loading all data before filter is better that loading the filtered data :)
Its better to fetch data using WHERE clause because it will boost your system performance in terms of time and load.
Because if you use "select * " then that will consume more time and after that you have to waste further time to use that records as per your needs. So again you have to write code for that.
Where is the better solution. If you would like to do it in php you have to load all data from database before you can filter the data.
In database you can add an index which makes the filtering performence better.
I think it also depends on how many time you need to call that mysql query to fetch result.
SO if you call that only once then yes WHERE clause is better solution
But if it require you to make multiple server call to fetch filterd data using mysql query then I think the first one approach will be better than this I.e Select all rows from a table and then filter the results with PHP
I hope it help you :)

PHP extract data via SQL or PHP?

I'm working with data bases using PHP and ODBC driver. And I make a SQL query. Now I need to print the result, but only unique items. As I assume there're two ways: rebuild my query usind DISTINCT clause and rebuild the result array like this: $uniques = array_unique($result, SORT_REGULAR);
And now I'm confused about what way is more correct (in terms of data processing or execution time etc.)
Thanks.
UPD. I have a huge database, but the result could contain < 10 rows
Always the best is to optimize your SQL query. Using DISTINCT you can save the time for bringing the unnecessary records and no need to waste time by removing duplicate using PHP too.
For speed and memory efficiency, you want to return the minimum amount from the database without putting unnecessary rows for processing/memory efficiency. So, the distinct in this case is the better choice.

MySQL sort result similar to .NET recordset

I need to migrate a code from .NET to MySQL and looking for an analog to the recordset SORT function. In my .NET code it was easy to create a recordset and run multiple sort operation across different fields. It was extremely fast even for large recordsets of over half a million records.
I understand that in MySQL I can use an ORDER BY clause, but in that case I will need to run multiple db queries, which is not a good solution from the performance point.
How can I sort a query result on different fields without executing multiple queries? PHP array sort functions or there are some better options?
Thanks!
ORDER BY works on multiple fields. Is that what you mean?
SELECT [column list] FROM table
ORDER BY field1, field2

How do I paginate items queried with mysql but sorted with PHP usort function?

I do a query to fetch all restaurants in my mysql db.
After that, I sort these on distance to the user with the PHP usort function, based on their lat/lng coordinates.
What's the most efficient way to paginate the usorted array, without needing to do a query and usort on all items each new paginated page?
You shouldn't be doing this in PHP. MySQL will do a much better job sorting your results set.
There's a paper here about doing this in MySQL, which is referenced here.
Regardless of which formula you're using, you should be able to do this with MySQL, even if you have to used stored procedures.

Categories