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>";
Related
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"
How do I get Direct access through the database query to product name , product price ,permalink , and image url of product in WordPress WooCommerce ?!
what's the sql query ?
global $wpdb;
$metas = array(
'_thumbnail_id', '_regular_price'
);
foreach ($metas as $i=>$meta_key) {
$meta_fields[] = 'm' . $i . '.meta_value as ' . $meta_key;
$meta_joins[] = ' left join ' . $wpdb->postmeta . ' as m' . $i . ' on m' . $i . '.post_id=' . $wpdb->posts . '.ID and m' . $i . '.meta_key="' . $meta_key . '"';
}
$request = "SELECT ID, post_title, " . join(',', $meta_fields) . " FROM $wpdb->posts ";
$request .= join(' ', $meta_joins);
$request .= " WHERE post_status='publish' AND post_type='product' AND $wpdb->posts.post_title like '%sample%'";
$product_details = $wpdb->get_row($request, OBJECT);
if(!empty($product_details)){
$qry = "SELECT guid FROM $wpdb->posts WHERE ID=" . $product_details->_thumbnail_id;
$uri_obj = $wpdb->get_row($qry, OBJECT);
$product_details->_thumbnail_id = $uri_obj->guid;
}
echo '<pre>';print_r($product_details);exit;
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';
here is my query i want to execute it as
$query="select * from $post INNER JOIN $postmeta on $postmeta.post_id= $post.ID
where $post .post_status='publish'
and $post .post_type ='task
and $post .post_type='service'
ORDER BY $post .post_date DESC ";
how to execute
Previous a proper sanitize of the var $postmeta and $post you can do this way
Building dinamic query by string concantenation (because var in tabel and column name are not allowed in sql)
$query="select * from " . $post . " INNER JOIN " .
$postmeta . " on " . $postmeta . ".post_id= " . $post .".ID where " .
$post . ".post_status='publish' and " . $post .".post_type ='task' and " .
$post . "post_type='service' ORDER BY " . $post .".post_date DESC ";
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";