Sum a subsection of a query result column? - php

I have an interesting problem and am having trouble figuring it out. I'm trying to optimize some SQL queries by handling some of the result separation with PHP rather than running multiple SQL queries. My query returns the following:
Using PHP, is there a way to sum the results by 'processor'? So, for example, I want be able to echo out '36.00' for PayPal and so forth. I'm currently able to do this with multiple individual queries, but am relatively confident there's a way to do this with one query and using PHP - I just can't figure out how to do it.
Thanks in advance for the help.

I think you just want a group by query:
select processor, sum(payment_amount)
from t
group by processor;
There is no reason to do this work in PHP.

Related

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.

Multiple Queries one PHP script

I have a page called profile.php I need information from multiple tables from within the same database.
Are multiple queries the way to deal with this, like this:
//connect
//query1
//query2
//query3
//query4
Mysqli_Query(connect, query1)
Mysqli_Query(connect, query2)
Mysqli_Query(connect, query3)
Mysqli_Query(connect, query4)
There is certainly nothing stopping you from making multiple queries, but depending on what you're doing, a JOIN is likely far more appropriate.
There's more to cover here than can reasonably fit in a StackOverflow answer, so please try a tutorial: http://www.tizag.com/mysqlTutorial/mysqljoins.php
Unless you're gathering data from query1 and using it in query2 and so on, you can optimize multiple queries into a single query by using UNION or JOIN. That's all I can offer without seeing any actual code.
what i get from your question is that there are many tables and you just need information from them, in that case it is better to go for UNION or JOIN over multiple queries as long as the tables are somehow related.

Trying to optimize this MySQL query (select, insert and update in the same query)

i have this query from php:
mysql_query("INSERT INTO ".table_hack." (id,ip,count,lasttime)
VALUES (NULL,'".$ip."',1,NOW())
ON DUPLICATE KEY UPDATE count=count+1, lasttime=NOW();");
As you can see im inserting a new record on the table_hack only if the Unique element is not present that in my case is ip. If the element is present the query updates de value of count in 1 and lasttime with the NOW() parameter.
I want also to know the value of count in the same query, it is possible?. I know that i could make another query but i will like to make it just in one (if is possible). Thanks for any help!
Nope, you can't.
Also, I see no reason for such a desire.
To me, it's interesting phenomenon. Such questions are quite often on Stackoverflow and I am curious why people so eager to combine queries into one call.
And even more surprising fact that everyone are limiting themselves with just 2 queries, and nobody asks how to combine ALL the queries from the script into one mysql call.
You can't combine an INSERT and SELECT query so you'll have to run two queries. Why would it be a problem to use two queries?
You can not do it with standard php mysql functions. You can do it with Mysqli driver and method multi_query(), but it will be a 2 queries whatever.

MySQL - large query vs Short query with multiple if's

This is a really broad question, but I have come across it a couple of times in the last few weeks and I was wondering what the general consensus is regarding good practice and efficiency.
1)
SELECT COUNT(*) FROM table WHERE id='$id', name='$name', owner='$owner_id'
and then based on if there is one result then the record matches.
2)
SELECT * FROM table WHERE id='$id'
and then a series of if commands to check the results match.
Now obviously there are advantages to the second solution as it allows for accurate error reports as to the field that does not match... but if that is not required which is more efficient, considered better practice and is there a difference to the load on the mySQL server between the two?
Option 1 by a long shot. Let SQL do what it is designed to do best, and better than procedural code. That is, filtering and sorting data.
Also, it is a much more efficient use of resources (bandwidth, DB utilization, etc) to pull down only the data you need from the server.
Use 1). Mysql is very efficient in selecting data based on certain conditions.
Large query can take .1 to 5.1 or more seconds, you need to find, run it and find it. Usually multiple if are way better as PHP is very fast. I did that when I was using it with 5 joins in table with 5 billion products, then I reduce one join and then use if statement to fix it up. Query was taking 4.2 seconds, when I reduced join, it took 3.8s but as you know PHP is way faster.

Categories