I try to combine a SELECT and an AVG query into one. My query is working but the AVG result is not shown as expected. Error is:
Undefined index
I thought I can combine AVG and SELECT like this?
the result will be a list and should also show an average progress value. If I put this AVG query separately and inside the "fetch" it works but shows only the first result. So it is not a solution for me because I will have much more rows.
I hope someone can help me to rebuild this miracle :)
<?php
$statement = $pdo->prepare("
SELECT
audit.id as audit_id,
audit.uid,
audit.assigned_auditor,
audit.audit_req_comment,
audit.audit_req_date,
audit.audit_date_start,
audit.general_audit_status,
audit.audit_request_date,
audit.audit_type,
audit.audit_date_start,
questionaire.quest_name,
users.nachname,
suppliers.supplier_name,
suppliers.supplier_city,
suppliers.supplier_country,
(SELECT AVG(progress) AS progress FROM answers WHERE relevant = '1' AND audit_id = :audit_id AND rating != required_answer)
FROM audit
JOIN users ON audit.uid = users.id
JOIN suppliers ON audit.supplier_id = suppliers.id
JOIN questionaire ON audit.questionaire_id = questionaire.id
WHERE
audit.cid = :cid
AND audit.general_audit_status = 'Maßnahmenplan'
AND audit.assigned_auditor = :assigned_auditor"
);
$result = $statement->execute(array(':cid' => $cid, ':assigned_auditor' => $user['id'], ':audit_id' => 4));
$count = 1;
while ($row = $statement->fetch()) {
// Datum umwandeln
$original_date = $row['audit_date_start'];
// Creating timestamp from given date
$timestamp = strtotime($original_date);
// Creating new date format from that timestamp
$new_date = date("d.m.Y", $timestamp);
// Audit Typ Namensgebung
if ($row['audit_type'] == "AR") {
$audit_type = "Externes Audit";
}
if ($row['audit_type'] == "RA") {
$audit_type = "Remote Audit";
}
if ($row['audit_type'] == "IA") {
$audit_type = "Internes Audit";
}
if ($row['audit_type'] == "SAA") {
$audit_type = "Self Assessment Audit";
}
//Berechne die durchschnittliche Abarbeitung aller Maßnahmen dieses Audits
//$statement = $pdo->prepare("SELECT AVG(progress) AS progress FROM answers WHERE relevant = '1' AND audit_id = :audit_id AND rating != required_answer");
//$statement->execute(array(':audit_id' => $row['audit_id']));
//$total_progress = $statement->fetch();
echo "<tr>";
echo "<td>" . $row['audit_id'] . "</td>";
echo "<td>" . $new_date . "</td>";
echo "<td>" . $row['supplier_name'] . "</td>";
echo "<td>" . $row['supplier_city'] . " (" . $row['supplier_country'] . ")</td>";
echo "<td>" . $row['quest_name'] . "</td>";
echo "<td>" . $audit_type . "</td>";
echo "<td>" . round($row['progress'], 0) . " %</td>";
echo '<td align="center"><i class="fas fa-edit"></i></td>';
echo "</tr>";
}
?>
As I understand your question, you just need to alias the results of the subquery in the outer query (rather than in the inner query itself):
SELECT
audit.id as audit_id,
audit.uid,
audit.assigned_auditor,
audit.audit_req_comment,
audit.audit_req_date,
audit.audit_date_start,
audit.general_audit_status,
audit.audit_request_date,
audit.audit_type,
audit.audit_date_start,
questionaire.quest_name,
users.nachname,
suppliers.supplier_name,
suppliers.supplier_city,
suppliers.supplier_country,
(
SELECT AVG(progress)
FROM answers
WHERE relevant = 1 AND audit_id = :audit_id AND rating != required_answer
) AS progress --> here
FROM audit
JOIN users ON audit.uid = users.id
JOIN suppliers ON audit.supplier_id = suppliers.id
JOIN questionaire ON audit.questionaire_id = questionaire.id
WHERE
audit.cid = :cid
AND audit.general_audit_status = 'Maßnahmenplan'
AND audit.assigned_auditor = :assigned_auditor"
Related
I am curious as to how best achieve this.
wpdb query 1:
$results = $wpdb->get_results(
"SELECT *
FROM $table_name
WHERE user_id = '$userid';"
);
I then want to do another wpdb query on a separate table using $results->post_id in the WHERE part of the query such as:
$results2 = $wpdb->get_results(
"SELECT *
FROM $table_name2
WHERE user_id = '$results->post_id' && type = '$type';"
);
I am conscious that $results will be an array so I don't think the above will work directly and some sort of compound "for each" seems cumbersome.
What are my options please?
Update example based on Jarlh's comment
$userid = get_current_user_id();
global $wpdb;
$table_name = $wpdb->prefix . 'postmeta';
$table2_name = $wpdb->prefix . 'posts';
$rewards_results = $wpdb->get_results(
"SELECT *
FROM $table_name
WHERE post_id in ("SELECT user_id
FROM $table2_name
WHERE user_id = '$userid';")"
);
Update 2:
$userid = get_current_user_id();
global $wpdb;
$table_name = $wpdb->prefix . 'postmeta';
$table2_name = $wpdb->prefix . 'posts';
$results = $wpdb->get_results(
"SELECT *
FROM $table_name
WHERE ID in (SELECT user_id
FROM $table2_name
WHERE user_id = '$userid')"
);
//return $results;
foreach ($results as $row) {
echo "<td class='dash-2-td'>" . esc_html($row->logo_id) . "</td>";
echo "<td class='dash-2-td'>" . esc_html($row->belongs_to) . "</td>";
echo "</tr>";
}
This is producing no output.
I need to search $table_name->post_id for rows that match $table2_name->ID where user_id = $userid
Update 3: print_r is just showing array(), seems empty
$userid = get_current_user_id();
global $wpdb;
//$table_name = $wpdb->prefix . 'postmeta';
//$table2_name = $wpdb->prefix . 'posts';
$results = $wpdb->get_results(
"SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_author = $userid
AND $wpdb->postmeta.plan = 3981
");
return $results;
I ended up using wp_query which I had not even considered as an option.
As a solution, you can use INNER JOIN. Also, I'd suggest you to use $wpdb->prepare for escaping values. Here is an example:
$sql = $wpdb->prepare( "SELECT a.*, b.col1, b.col2 FROM $table1 as a INNER JOIN $table2 as b ON a.user_id = b.user_id WHERE a.user_id = %d AND b.type = %s", $user_id, $type );
$entries = $wpdb->get_results( $sql, ARRAY_A );
Hope it helps.
I am trying to translate this query to CActiveDataProvider:
// SELECT p.id "
// . "FROM projects AS p "
// . "LEFT JOIN purged_files AS pf ON p.id = pf.project_id "
// . "WHERE pf.id IS NULL "
// . "AND `new_status_id` IN ('DELIVERED', 'PAID') "
// . "AND `created` <= '" . date("Y-m-d H:i:s", strtotime("-" . $lifetime_days . " DAYS")) . "' "
// . "ORDER BY id DESC "
// . "LIMIT 0, 5000")
Current code:
$criteria->select = "project.id";
$criteria->join = 'LEFT JOIN purged_files ON project.id = purged_files.project_id';
$criteria->order = "project.id desc";
$criteria->condition = "`new_status_id` IN ('DELIVERED', 'PAID') AND `created` <= '" . date("Y-m-d H:i:s", strtotime("-180 DAYS")) . "'";
$criteria->limit = 5000;
$criteria->offset = 0;
$dataProvider = new CActiveDataProvider('Project', array(
'criteria' => $criteria,
));
$iterator = new CDataProviderIterator($dataProvider);
foreach ($iterator as $project) {
echo $project->id . "<br>";
}
var_dump('<pre>', "end", '</pre>');die;
When I run this I am getting this error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'project.id' in 'on clause'. The SQL statement executed was: SELECT COUNT(*) FROM `projects` `t` LEFT JOIN purged_files ON project.id = purged_files.project_id WHERE (`t`.`status` <> "DELETED" AND `t`.`status` <> "CANCELED" AND `t`.`status` <> "ARCHIVED") AND (`new_status_id` IN ('DELIVERED', 'PAID') AND `created` <= '2017-12-29 13:31:42')
What seems to be the issue here?
You're using incorrect alias in your conditions. Main model table is aliased as t, so either you should use t as alias in your condition instead of project:
$criteria->order = "t.id desc";
Or overwrite alias in criteria:
$criteria->alias = 'project';
I have the below code where I am trying to get a list of orders.
So from the wcomm (woocommerce_order_itemmeta) table, I only want to display the meta_value where the meta_key is equal to _qty or _product_id and I need the _product_id to be filtered on a single value like I have. I can't seem to make it only do both things.. My output never shows the _qty value because I'm specifically filtering the value of _product_id but I don't understand what to change
global $wpdb;
$wpdb->woocommerce_order_items = $wpdb->prefix . 'woocommerce_order_items';
$wpdb->woocommerce_order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$uvuorderposts = $wpdb->get_results(
"
SELECT p.ID, p.post_title, p.post_date, wcom.order_item_name, wcomm.meta_key, wcomm.meta_value
FROM $wpdb->posts AS p
INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
INNER JOIN $wpdb->woocommerce_order_items AS wcom ON p.ID = wcom.order_id
INNER JOIN $wpdb->woocommerce_order_itemmeta AS wcomm ON wcom.order_item_id = wcomm.order_item_id
WHERE (p.post_type = 'shop_order')
AND (pm.meta_key = '_wc_authorize_net_aim_charge_captured')
AND (pm.meta_value = 'yes')
AND (wcomm.meta_key = '_product_id')
OR (wcomm.meta_key = '_qty')
AND (wcomm.meta_value = '2193')
ORDER BY p.post_date
"
);
echo "<table>";
foreach ( $uvuorderposts as $uvuorderpost )
{
echo "<tr><td>" . $uvuorderpost->post_title . " - " . $uvuorderpost->ID . "</td><td>" . $uvuorderpost->post_date . "</td><td>" . $uvuorderpost->meta_key . ": " . $uvuorderpost->meta_value . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='3'>" . $uvuorderpost->order_item_name . "</td></tr>";
}
echo "</table>";
I tried to "filter" it on the output by using $uvuorderpost->meta_key['_qty'] but it did not work!
I fixed it with the following changes. Just joined that table twice and it works now. Not sure if it's best practice but yeah
global $wpdb;
$wpdb->woocommerce_order_items = $wpdb->prefix . 'woocommerce_order_items';
$wpdb->woocommerce_order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$uvuorderposts = $wpdb->get_results(
"
SELECT p.ID, p.post_title, p.post_date, wcom.order_item_name, wcomm.meta_key, wcomm.meta_value, wcomm2.meta_key AS meta_key_qty, wcomm2.meta_value AS meta_value_qty
FROM $wpdb->posts AS p
INNER JOIN $wpdb->postmeta AS pm ON p.ID = pm.post_id
INNER JOIN $wpdb->woocommerce_order_items AS wcom ON p.ID = wcom.order_id
INNER JOIN $wpdb->woocommerce_order_itemmeta AS wcomm ON wcom.order_item_id = wcomm.order_item_id
INNER JOIN $wpdb->woocommerce_order_itemmeta AS wcomm2 ON wcom.order_item_id = wcomm2.order_item_id
WHERE (p.post_type = 'shop_order')
AND (pm.meta_key = '_wc_authorize_net_aim_charge_captured')
AND (pm.meta_value = 'yes')
AND (wcomm.meta_key = '_product_id')
AND (wcomm.meta_value = '2193')
AND (wcomm2.meta_key = '_qty')
ORDER BY p.post_date
"
);
echo "<table>";
foreach ( $uvuorderposts as $uvuorderpost )
{
echo "<tr><td>" . $uvuorderpost->post_title . " - " . $uvuorderpost->ID . "</td><td>" . $uvuorderpost->post_date . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='2'>" . $uvuorderpost->order_item_name . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='2'>" . $uvuorderpost->meta_key . " : " . $uvuorderpost->meta_value . "</td></tr>";
echo "<tr bgcolor='#eaeaea'><td colspan='2'>" . $uvuorderpost->meta_key_qty . " : " . $uvuorderpost->meta_value_qty . "</td></tr>";
}
echo "</table>";
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to use all columns for each row from one table (party), and some columns from another (customer).
Ok, here is my code
public function getParty($data = array()) {
$sql = "SELECT party.* , customer.customer_id AS customerID, customer.firstname AS customerFN, customer.lastname AS customerLN, customer.email AS customerEMAIL, customer.ip AS cutomerIP
FROM `" . DB_PREFIX . "party`
JOIN `" . DB_PREFIX . "cutomer`
ON party.cutomer_id = customer.customer_id
WHERE status=3";
$sort_data = array(
'date_added',
'order_id',
'customer_id',
'customerFN',
'customerLN',
'customerEMAIL',
'customerIP',
'description',
'status',
'amount'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY date_added";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
And here is the error
Notice: Error: Table 'XXX.cutomer' doesn't exist
Error No: 1146
SELECT party.* , customer.customer_id AS customerID, customer.firstname AS customerFN, customer.lastname AS customerLN, customer.email AS customerEMAIL, customer.ip AS cutomerIP FROM `party` JOIN `cutomer` ON party.cutomer_id = customer.customer_id WHERE status=3 ORDER BY date_added ASC LIMIT 0,10 in /srv/www/htdocs/XXX/system/database/mysql.php on line 49
Maybe I am wrong somewhere, but do not see it
Greetings
Petar Stoyanov
JOIN `" . DB_PREFIX . "cutomer`
Should be
JOIN `" . DB_PREFIX . "customer`
?
Also...
ON party.cutomer_id = customer.customer_id
Should be
ON party.customer_id = customer.customer_id
?
I would search for all instances of cutomer and replace them with customer.
I think it should be
ON party.customer_id = customer.customer_id
instead of
ON party.cutomer_id = customer.customer_id
Please change from
JOIN `" . DB_PREFIX . "cutomer`
To
JOIN `" . DB_PREFIX . "customer`
Ex:
FROM `" . DB_PREFIX . "party`
JOIN `" . DB_PREFIX . "customer`
ON party.cutomer_id = customer.customer_id
WHERE status=3";
Change your SQL stmt to this:
$sql = "SELECT party.* , customer.customer_id AS customerID, customer.firstname AS customerFN, customer.lastname AS customerLN, customer.email AS customerEMAIL, customer.ip AS customerIP
FROM `" . DB_PREFIX . "party`
JOIN `" . DB_PREFIX . "customer`
ON party.customer_id = customer.customer_id
WHERE status=3";
I want to get the recent tags from last 10 posts. Or get the recent 100 tags which was used in last posts.
My code :
<?php
$tags .= "SELECT $wpdb->terms.term_id, $wpdb->terms.name, $wpdb->terms.slug
FROM $wpdb->terms
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
INNER JOIN $wpdb->term_relationships ON ($wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id)
INNER JOIN $wpdb->posts ON ($wpdb->term_relationships.object_id = $wpdb->posts.ID)
WHERE $wpdb->term_taxonomy.taxonomy = 'post_tag'
ORDER BY $wpdb->posts.post_date DESC";
$tags= $wpdb->get_results($tags); // $query being the above SQL
foreach ($tags as $tag) {
if (!isset($stack[$tag->term_id]))
$stack[$tag->term_id] = $tag;
}
print_r($stack); // should print an array of all tags, ordered by last used
?>
And it is not working. For example there is no links to tag.
Check if your query is correct or not.
print_r($tags);
and see if you get any data or not?
This is a sample code for tag. Create query and pass it to $posttags variable to get your required result.
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
?>
How about this:
<?php
function my_tags_test ()
{
?>
<div style="background: white; color: black; font-size: 15px;">
<h1 style="font-size: 100px;">my_tags_test:</h1>
<?php
global $wpdb;
$sql =
'
drop temporary table if exists last_10_posts
;';
$wpdb->query ($sql);
$sql =
'
create temporary table last_10_posts (ID int, post_date datetime)
;';
$wpdb->query ($sql);
$sql =
'
insert into last_10_posts
select
ID, post_date
from
' . $wpdb->posts . '
order by
post_date desc
limit 0, 10
;';
$wpdb->query ($sql);
$sql =
'
SELECT terms.term_id, terms.name, terms.slug
FROM ' . $wpdb->terms . ' terms
INNER JOIN ' . $wpdb->term_taxonomy . ' tt
ON (terms.term_id = tt.term_id)
INNER JOIN ' . $wpdb->term_relationships . ' tr
ON (terms.term_id = tr.term_taxonomy_id)
INNER JOIN ' . $wpdb->posts . ' posts
ON (tr.object_id = posts.ID)
INNER JOIN last_10_posts
ON (last_10_posts.ID = posts.ID)
WHERE
tt.taxonomy = \'post_tag\'
ORDER BY
posts.post_date DESC
';
$tags = $wpdb->get_results ($sql); // $query being the above SQL
$stack = array ();
$links = '';
foreach ($tags as $tag)
{
if (!isset($stack[$tag->term_id]))
{
$stack[$tag->term_id] = $tag;
}
$links .= '→ ' . esc_html ($tag->name) . '<br/>';
}
echo '<div style="margin: 20px; padding: 20px;">' . $links . '</div>';
echo '<pre>' . esc_html (print_r ($stack, true)) . '</pre>'; // should print an array of all tags, ordered by last used
?>
</div>
<?php
}
add_action ('wp_footer', 'my_tags_test');
?>