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)
Related
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 do search function In raw SQL like this
//test condition variable
if ($param_type != "") {
if($param_type =='SAS'){
$str .= " AND pos.type IN('SAS1','SAS2') ";
}
else if($param_type =='SME'){
$str .= " AND pos.type IN('SME1','SME2') ";
}
else if($param_type =='PT'){
$str .= " AND pos.type IN('PT','PT0') ";
}else{
$str .= " AND pos.type ='$param_type'";
}
}
//SQL
$query =" SELECT * FROM tbl_user AS u INNER JOIN tbl_position AS pos
ON u.user_id = pos.user_id where u.levels IN ('7','8','9') .$str ;" ;
How I can write SQL like this in laravel ?
You can use raw query.
$query = DB::select(SELECT * FROM tbl_user AS u INNER JOIN tbl_position AS pos
ON u.user_id = pos.user_id where u.levels IN ('7','8','9') .$str ;");
Of course then you need to add at the top in your controller use DB;
Anyway Raw queries may be dangerous and it's better to use query builder.
Simple example:
$query = DB::table('tbl_user')
->join('tbl_position','tbl_user.user_id','=','tbl_position.user_id')
->where('something','=','something')
->get();
Query builder: query builder
Reply to my question for who meet problem like me:
Example like this
$query = DB::table('user');
if ($param_type =='PT')
$query->where('pos.type', '=', 1);
}
$result = $query->get();
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 ";
I am trying to get this inner join to show output the the title and date. Is only returning the sql object that gives details on object. this is result. I can access num_rows but just gives me int(11) for the number of records, don't know how to access the info in the fields.
This is the result.
object(mysqli_result)#6 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(11) ["type"]=> int(0) }
Thanks for help.
$sql = "SELECT title, artist_name";
$sql .= " FROM follows";
$sql .= " INNER JOIN artworks";
$sql .= " ON follows.user_id_followed = artworks.artist_id";
$sql .= " AND follows.user_id_follower='12'";
$result_set = $database->query($sql);
echo var_dump($result_set);
you need a WHERE
$sql = "SELECT title, artist_name";
$sql .= " FROM follows";
$sql .= " INNER JOIN artworks";
$sql .= " ON follows.user_id_followed = artworks.artist_id";
$sql .= " WHERE follows.user_id_follower='12'";
You also probably want LEFT JOIN not INNER JOIN`
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>";