I'm looking for a little help with the best way to get results to display the way I want from as few as possible database queries.
I would like to use one of my queries results columns as new unique columns for another table and merge like data.
For example
I run this query..
SELECT pd.production_date,pd.production_time,pd.production_net,
p.part_number, w.worker_name
FROM production as pd
INNER JOIN parts as p
ON pd.product_id = p.part_id
INNER JOIN locations as l
ON pd.location_id = l.location_id
INNER JOIN workers as w
ON pd.worker_id = w.worker_id
WHERE pd.production_date BETWEEN '$from' AND '$to'
AND p.part_number='$part_number'
ORDER BY pd.production_date
And I will get results like this:
production_date--production_time--production_net--worker_name
2013-01-10--390--96--MMahe
2013-01-10--400--101--RMaloney
2013-01-11--460--96--JBurris
2013-01-11--210--43--MMahe
2013-01-14--285--48--LTaylor
2013-01-16--60--8--MRocquemore
2013-01-16--460--90--TUsher
2013-01-17--450--85--MRocquemore
2013-01-17--460--84--TUsher
2013-01-18--450--73--MRocquemore
2013-01-18--460--93--TUsher
2013-01-18--240--24--JBurris
I then would like to display these results on a webpage like this:
Operators|01-10-2013|01-11-2013|01-14-2013|01-16-2013|01-17-2013|01-18-2013
MMahe|96|43|x|x|x|x
RMaloney|101|x|x|x|x|x
JBurris|x|96|x|x|x|24
LTaylor|x|x|48|x|x|x
MRocquemore|x|x|x|8|85|73
TUsher|x|x|x|90|84|93
My thought was first I need to filter my days to unique values then spit them out as the columns, which I have accomplished with this
$query=mysql_query($sql);
$days = array();
$results.='<div><table border="0"><th>Operator</th>';
while($row=mysql_fetch_array($query)) {
array_push($days,$row['production_date']);
}
$days=array_unique($days);
$column_count=count($days);
$n=0;
//create the columns (days) by looping through unique days
foreach($days as $value) {
$results.='<th>'.$value.'</th>';
}//end foreach
But now I don't see a way forward of listing values in the appropriate cells.
Do you have experience with doing this sort of data manipulation or can point me to a good example?
Related
I need to obtain the sum of many values with the same key from a SQL Query.
For example...say I have 30 rows returned from a Query and they look like this.
1) (a:2), (b:4),(f:1), (h:3)
2) (c:2), (f:4),(t:1), (z:3)
3) (a:5), (b:2),(s:1), (z:3)
4) (d:2), (g:4),(s:1), (t:3)
and so on....
What I need to do is sum up all the "a" and all the "b" and all the "c" etc
And put all that into an array or object so I can access them for later processing.
$total = [(a:7),(b:6),(c:2),(d:2).....(z:6)];
Is there a way to do this all from SQL? Or do I have to sum all these records externally using PHP for example?
This is my SQL code to obtain the 30 records...
while($row = mysql_fetch_array($rs)) {
$queryActivity = "SELECT activity, count(activity) FROM users u, activities_users au, activities a WHERE ($row[zip] = u.zip AND u.id = au.user_id AND au.activity_id = a.id) GROUP BY a.activity";
$rs2 = mysql_query($queryActivity);
while($row2 = mysql_fetch_array($rs2)) {
echo "<tr><td>$row2[0]</td><td>";
echo "<tr><td>$row2[1]</td><td>";}
It returns all the activities and the sum of those activities for a certain zipcode. I just need to total all the sums up for each activity.
Is (a:2), (b:4)... is a row in the database?
You will need to use some sort of split, SUM, and GROUP BY's. It will be hard to help until we have more information.
So here is a quick query that will group by the first subset of the rows. You can do the same thing for every other subset. I don't know of a way to do what you want to do in pure mysql.
SELECT
SUBSTRING(activity, 1,2),
count(activity)
FROM users u, activities_users au, activities a
WHERE ($row[zip] = u.zip AND u.id = au.user_id AND au.activity_id = a.id)
GROUP BY SUBSTRING(activity, 1,2)
I am not sure I fully understand your requirement, but you can use the SUM() method in sql.
SELECT SUM(column_name) FROM table GROUP BY column_name
Add WITH ROLLUP to your query after GROUP BY clause:
SELECT activity, count(activity)
FROM users u, activities_users au, activities a
WHERE ($row[zip] = u.zip AND u.id = au.user_id AND au.activity_id = a.id)
GROUP BY a.activity WITH ROLLUP
It will add an extra row (null, totalsum) to the result set which you need to handle in PHP.
As alternative, you can calculate it with PHP inside your while loop:
$total[$row2[0]] = (empty($total[$row2[0]]) ? 0 : $total[$row2[0]]) + $row2[1]
I'm using this to display information from a queried db in Wordpress. It displays the correct information but it loops it too many times. It is set to display from a SELECT query and depending on the last entry to the db seems to be whether or not it prints double or triple each entry.
foreach ($result as $row) {
echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'</i><br>';
echo 'This price includes using<i> '.$row->material.'</i> as your material(s)<br>';
echo '<hr></h5>';
}
Does anyone know what could be producing this error?
Thanks
The query powering that script is:
$result = $wpdb->get_results( "SELECT bp.*, b.company
FROM `windows_brands_products` bp
LEFT JOIN `windows_brands` b
ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
JOIN windows_materials wm
JOIN Windows_submissions ws
WHERE ws.username = '$current_user->user_login'
AND bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material IN (wm.name)
AND bp.type = ls.type
AND IF (ls.minimumbid != '0.00',bp.cost BETWEEN ls.minimumbid AND ls.maximumbid,bp.cost <= ls.maximumbid)
ORDER BY b.company ASC");
I can't seem to see the duplicate but I agree it must be there.
EDIT-- when I replace the WHERE clause to WHERE ws.username = 'password' , it still repeats. It it displaying a result for each time a result has username='password' , and displaying that set twice as well.
I think you want the following, if you're using MySQLi:
while ($row = $result->fetch_object()) {
echo '<h5><i>'.$row->company.'</i> can perform your window installation for <i>$'.$row->cost.'</i><br>';
echo 'This price includes using<i> '.$row->material.'</i> as your material(s)<br>';
echo '<hr></h5>';
}
Redundant JOIN clauses in my query which was pretty much pulling the same results from two tables (one of which was just a VIEW of the other).
I'm trying to display and sort an array by an average created using data from a database. I'm retrieving three variables from the database and creating an average from these values. This value is then placed inside a new array to be sorted along with the rest of the database data.
Am I right in thinking that having the SQL query inside the loop isn't a great idea? (Performance issue?)
Is there any alternative that's available? I've attached the code below:
^ database connection/query string to retrieve all data...
$result = $stmt_business_list->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach($result as $row){
$single_business_id = $row['id'];
$name = $row['name'];
//Query to get ALL the service, value and quality ratings for certain business
$test_query = "SELECT * FROM rating WHERE business_id = $single_business_id";
$test_query_stmt = $dbh->prepare($test_query);
$test_query_stmt->execute();
$test_row = $test_query_stmt->fetchAll(PDO::FETCH_ASSOC);
$total_value = $total_quality = $total_service = 0;
foreach($test_row as $review)
{
$total_value += $review['value'];
$total_quality += $review['quality'];
$total_service += $review['service'];
}
$bayesian_value = (($set_site_average_review_count * $set_site_average_review_score) + $total_value) / ($set_site_average_review_count + $business_review_count);
$bayesian_quality = (($set_site_average_review_count * $set_site_average_review_score) + $total_quality) / ($set_site_average_review_count + $business_review_count);
$bayesian_service = (($set_site_average_review_count * $set_site_average_review_score) + $total_service) / ($set_site_average_review_count + $business_review_count);
$average_bayesian_rating = ($bayesian_value + $bayesian_quality + $bayesian_service) / 3;
$average_bayesian_rating = $average_bayesian_rating;
array_push($items, array(
"id"=>"$single_business_id",
"name"=>"$name",
"value"=>"$total_value",
"quality"=>"$total_quality",
"service"=>"$total_service",
"average"=>"$average_bayesian_rating"));
echo
'Name: '.$name.'<br>
Value: '.$total_value.'<br>
Quality: '.$total_quality.'<br>
Service: '.$total_service.'<br>
Average: '.$average_bayesian_rating.'<br><br>';
}
}
The page will be split up by a separate pagination script and will only display 6 objects at a time, but over time this may change so I do have an eye on performance as much as I can.
SQL aggregate queries are made for this kind of thing.
Use this query to summarize the results
SELECT b.name, b.id,
SUM(value) total_value,
SUM(quality) total_quality,
SUM(service) total_service,
COUNT(*) review_count,
avg_reviews_per_biz
FROM business b
JOIN ratings r ON b.id = r.business_id
JOIN (
SELECT COUNT(DISTINCT business_id) / COUNT(*) avg_reviews_per_biz
FROM ratings
) a ON 1=1
GROUP BY b.name, b.id, avg_review_per_biz
This will give you one row per business showing the summed ratings and the number of ratings. This result set will have the following columns
name business name
id business id
total_value sum of value ratings for that business
total_quality sum of quality ditto
total_service sum of service ditto
review_count number of reviews for business "id"
avg_reviews_per_biz avg number of reviews per business
The last column has the same value for all rows of your query.
You can then loop over these row one business at a time doing your statistical computations.
I can't tell from your question where you're getting variables like $set_site_average_review_count, so I can't help with those computations.
You'll find that SQL aggregate querying is very powerful indeed.
I've been using the below php and sql for loading schedule information and real time information for passenger trains in the UK. Essentially you have to find the relevant schedules, and then load the realtime information for each schedule which is in a different table relating to todays trains.
The query is taking a little longer than is really idea and using lots of CPU% which again isn''t ideal. I'm pretty weak when it comes to sql programming so any pointers as to what is inefficient would be great.
This is for an android app and so i've tried to all with one call over http. The prints(*) and > is for splitting the string at the other end.
Here is the code:
<?
//Connect to the database
mysql_connect("localhost","XXXX","XXXX")
or die ("No connection could be made to the OpenRail Database");
mysql_select_db("autotrain");
//Set todays date from system and get HTTP parameters for the station,time to find trains and todays locations table.
$date = date('Y-m-d');
$test = $_GET['station'];
$time = $_GET['time'];
$table = $_GET['table'];
//Find the tiploc associated with the station being searched.
$tiplocQuery = "SELECT tiploc_code FROM allstations WHERE c LIKE '$test';";
$tiplocResult =mysql_query($tiplocQuery);
$tiplocRow = mysql_fetch_assoc($tiplocResult);
$tiploc=$tiplocRow['tiploc_code'];
//Now find the timetabled trains for the station where there exists no departure information. Goes back two hours to account for any late running.
$timeTableQuery = "SELECT tiplocs.tps_description AS 'C', locations$table.public_departure, locations$table.id,schedules.stp_indicator
,schedules.train_uid
FROM locations$table, tiplocs, schedules_cache, schedules,activations
WHERE locations$table.id = schedules_cache.id
AND schedules_cache.id = schedules.id
AND schedules.id =activations.id
AND '$date'
BETWEEN schedules.date_from
AND schedules.date_to
AND locations$table.tiploc_code = '$tiploc'
AND locations$table.real_departure LIKE '0'
AND locations$table.public_departure NOT LIKE '0'
AND locations$table.public_departure >='$time'-300
AND locations$table.public_departure <='$time'+300
AND schedules.runs_th LIKE '1'
AND schedules_cache.destination = tiplocs.tiploc
ORDER BY locations$table.public_departure ASC
LIMIT 0,30;";
$timeTableResult=mysql_query($timeTableQuery);
while($timeTablerow = mysql_fetch_assoc($timeTableResult)){
$output[] = $timeTablerow;
}
//Now for each id returned in the timetable, get the locations and departure times so the app may calculate expected arrival times.
foreach ($output as $value) {
$id = $value['id'];
$realTimeQuery ="SELECT locations$table.id,locations$table.location_order,locations$table.arrival,locations$table.public_arrival,
locations$table.real_arrival,locations$table.pass,locations$table.departure,locations$ table.public_departure,locations$table.real_departure,locations$table.location_cancelled,
tiplocs.tps_description FROM locations$table,tiplocs WHERE id =$id AND locations$table.tiploc_code=tiplocs.tiploc;";
$realTimeResult =mysql_query($realTimeQuery);
while($row3 = mysql_fetch_assoc($realTimeResult)){
$output3[] = $row3;
}
print json_encode($output3);
print("*");
unset($output3);
unset($id);
}
print('>');
print json_encode($output);
?>
Many Thanks
Matt
The biggest issue with your setup is this foreach loop because it is unnecessary and results in n number of round trips to the database to execute a query, fetch and analyze the results.
foreach ($output as $value) {
Rewrite the initial query to include all of the fields you will need to do your later calculations.
Something like this would work.
SELECT tl.tps_description AS 'C', lc.public_departure, lc.id, s.stp_indicator, s.train_uid,
lc.id, lc.location_order, lc.arrival, lc.public_arrival, lc.real_arrival, lc.pass, lc.departure, lc.real_departure, lc.location_cancelled
FROM locations$table lc INNER JOIN schedules_cache sc ON lc.id = sc.id
INNER JOIN schedules s ON s.id = sc.id
INNER JOIN activations a ON s.id = a.id
INNER JOIN tiplocs tl ON sc.destination = tl.tiploc
WHERE '$date' BETWEEN schedules.date_from AND schedules.date_to
AND lc.tiploc_code = '$tiploc'
AND lc.real_departure LIKE '0'
AND lc.public_departure NOT LIKE '0'
AND lc.public_departure >='$time'-300
AND lc.public_departure <='$time'+300
AND s.runs_th LIKE '1'
ORDER BY lc.public_departure ASC
LIMIT 0,30;
Eliminating n query executions from your page load should dramatically increase response time.
Ignoring the problems with the code, in order to speed up your query, use the EXPLAIN command to evaluate where you need to add indexes to your query.
At a guess, you probably will want to create an index on whatever locations$table.public_departure evaluates to.
http://dev.mysql.com/doc/refman/5.0/en/using-explain.html
A few things I noticed.
First, you are joining tables in the where clause, like this
from table1, table2
where table1.something - table2.something
Joining in the from clause is faster
from table1 join table2 on table1.something - table2.something
Next, I'm not a php programmer, but it looks like you are running similar queries inside a loop. If that's true, look for a way to run just one query.
Edit starts here
This is in response to gazarsgo's that I back up by claim about joins in the where clause being faster. He is right, I was wrong. This is what I did. The programming language is ColdFusion:
<cfsetting showdebugoutput="no">
<cfscript>
fromtimes = ArrayNew(1);
wheretimes = ArrayNew(1);
</cfscript>
<cfloop from="1" to="1000" index="idx">
<cfquery datasource="burns" name="fromclause" result="fromresult">
select count(distinct hscnumber)
from burns_patient p join burns_case c on p.patientid = c.patientid
</cfquery>
<cfset ArrayAppend(fromtimes, fromresult.executiontime)>
<cfquery datasource="burns" name="whereclause" result="whereresult">
select count(distinct hscnumber)
from burns_patient p, burns_case c
where p.patientid = c.patientid
</cfquery>
<cfset ArrayAppend(wheretimes, whereresult.executiontime)>
</cfloop>
<cfdump var="#ArrayAvg(fromtimes)#" metainfo="no" label="from">
<cfdump var="#ArrayAvg(wheretimes)#" metainfo="no" label="where">
I did ran it 5 times. The results, in milliseconds, follow.
9.563 9.611
9.498 9.584
9.625 9.548
9.831 9.769
9.792 9.813
The first number represents joining in the from clause, the second joining in the where clause. The first number is lower only 60% of the time. Had it been lower 100% percent of the time, it would have shown that joining in the from clause is faster, but that' not the case.
I’ve a small piece of code I’ve tried to wrap my brain around for some hours now.
I’m trying to create a query which retrieves some elements and in the end group them by a selector.
I’ll try to demonstrate: I’m querying all the members in my organization. Each member is a member of a “department” and there can be several members in each department, but only one department for each member.
So I’ve created the query to select all members in my organization group by departments.
When I’m displaying it using a simple foreach($my_query as $q) … I want to output $q->department_name but only once for that department. Right now it says “Marketing” under all five members and then it changes to “HR” for all those members. I’ve tried some different methods such as array_unique, creating a function that checks for the string to see if it is the same and other but with no result.
I could create two foreach loops but if it is at all possible not to I would prefer that because two foreach loops would affect the performance.
Any help or suggestion would be very much appreciated.
Sinerely
- Mestika
--- Edited ---
By the way, my query looks like this:
SELECT *
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status = 'publish'
AND meta.meta_key = 'active'
AND meta.meta_value = 'active'
AND cat_posts.post_type = 'member'
AND cat_term_taxonomy.taxonomy = 'deparment'
GROUP BY cat_terms.slug, cat_term_relationships.object_id
$prev_department = NULL;
foreach ($my_query AS $q)
{
if ($prev_department != $q->department_name)
echo '<h2>'. htmlentities($q->department_name, ENT_COMPAT, 'UTF-8') .'</h2>';
// ...
$prev_department = $q->department_name;
}