same table same field with same field conditions - Possible? in one sql - sql

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 ";

Related

How to Direct access through the database query to product name , product price and permalink and product photo columns

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;

Yii1 - CActiveDataProvider criteria not working as expected

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';

wpdb select query to get certain meta_key?

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>";

SQL query with AND/OR logical operators

fI have what seems to be a relatively simple AND/OR clause that is giving me a bit of a headache.
$query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
$query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
$query .= " LEFT JOIN $tb_content tb_content ON stc.contentid=tb_content.id ";
$query .= " WHERE stv.name='".$region."' AND stc.contentid IN (".implode($cIDs,",").") ";
$query .= " OR stv.name='".$countries."' AND stc.contentid IN (".implode($cIDs,",").") ";
$query .= " AND tb_content.pub_date >= '$pub_date' ";
$query .= " AND tb_content.published = 1 ";
$query .= " ORDER BY stc.contentid ASC;";
I need it to retrieve the values from both $region and $countries. However, the way it is it only returns the value of $countries. If I remove the line with the OR it returns the value of $region, but it won't return both. What am I doing incorrectly?
Try putting brackets around the clauses on either side of the OR. See below:
$query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
$query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
$query .= " LEFT JOIN $tb_content tb_content ON stc.contentid=tb_content.id ";
$query .= " WHERE (stv.name='".$region."' AND stc.contentid IN (".implode($cIDs,",").")) ";
$query .= " OR (stv.name='".$countries."' AND stc.contentid IN (".implode($cIDs,",").")) ";
$query .= " AND tb_content.pub_date >= '$pub_date' ";
$query .= " AND tb_content.published = 1 ";
$query .= " ORDER BY stc.contentid ASC;";
Looks like you are missing parentheses, no?
A or B and C or D is ambiguous, you need to use parentheses to make it clear you mean (A or B) and (C or D)

getting data from 2 different table with JOIN sql. Codeigniter

I use codeigniter, and I need to get data from 2 different table. for now it returns data only from works_image table. how can I get data from both 2 table ?
thanks a lot!!
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "";
}
As long as you're doing a SELECT * (Why is this a bad idea?), you shouldn't need to specify tables with the call to select(). It will select all fields by default.
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
Should work fine.
Instead, what you really should be doing, is specifying exactly which fields you need:
$this->db->select('works_image.id, works_image.name, works_image.id_work, works.id, works.name'); // (or whichever fields you're interested in)
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
This way you can be sure that (a) you're not pulling unnecessary data from your DB, and (b) your code won't break if/when you modify your DB schema.
This post should answer your question: http://www.whypad.com/posts/codeigniter-activerecord-join-tip/178/
In short, you need to rewrite your select statement from
$this->db->select('works_image.*', 'works.*');
to this:
$this->db->select('works_image.*, works.*');
Note that this was the first result on Google for 'CodeIgniter join'. Try Googling your questions first. You can often get yourself a faster answer :)
You should be to write below code
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "
";
}
I think that you get your result