sql query show error as near order by - mysql

$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "'group by pov.products_options_values_name, order by pov.products_options_values_id desc");
here code shows error as
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by pov.products_options_values_id desc' at line 1

Remove Comma after products_options_values_name . your query should as below
$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "' group by pov.products_options_values_name order by pov.products_options_values_id desc");

$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "' group by pov.products_options_values_name order by pov.products_options_values_id desc");

Remove Comma after Group By Clause
Try this
$products_options_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name, pa.options_values_price, pa.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pa.options_id = '" . (int)$products_options_name['products_options_id'] . "' and pa.options_values_id = pov.products_options_values_id and pov.language_id = '" . (int)$languages_id . "' group by pov.products_options_values_name order by pov.products_options_values_id desc");

Related

include wholesale field in mysql query

I am using Opencart 2.2.0. I have two customer groups - default and wholesale customers. Throughout my web site, price field is for default group, and wholesale field is for wholesale customer group. I also added wholesale field in specials tab (to the oc_product_special table) and everything works fine(it remembers the value in admin, as well as gets added to cart by the special price for both groups on front end.) My only problem is that it doesn't show correctly on front end. Meaning that it shows value from the special price field for both customer groups, instead of showing wholesale special price for wholesale customer. I figured out where the problem is - in the query in model/catalog/product file. I would like to know how to alter the following query, so that I can include wholesale field, because only then would I be able to show it on front end. My query is:
$query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, **(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special,** (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews, p.sort_order FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
The part of the query which is for special is:
(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special,
Thanx
I solved this problem...In case anyone gets stuck in the same place, here is what I did:
I added another line(subquery) to the query, where I defined special_wholesale like this:
SELECT wholesale FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.wholesale ASC LIMIT 1) AS special_wholesale
as well as defined 'special_wholesale' => $query->row['special_wholesale'], in query array bellow the 'special' array element in catalog/model/catalog/product.php file inside the public function getProduct($product_id) function.
After that, I defined $special_wholesale variable in controller file for the featured products(/catalog/controller/module/featured.php).
The last thing I did was call the $product['special_wholesale']; in featured.php(/catalog/view/theme/your-theme-name/template/module/featured.tpl) file like this:
<font size="2" color="red"> <span class="price-new"><?php echo "VP:" . $product['special_wholesale']; ?></span> <span class="price-old"><?php echo "VP:". $product['wholesale']; ?></span></font></br>
The output is: striked out old wholesale price and display of special wholesale price.
I hope this can help someone. Cheers!

syntax error (missing operator) in query expression sri.invoiceNo = sri.invoiceNo INNER JOIN Staff s oN s.Staffid = sr.userld

"SELECT SalesReturnId, ReturnDate, sr.InvoiceNo, (lastname & ', ' & firstname & ', ' & MI) as StaffName, TotalAmount, SUM(sri.Quantity) as TotalQuantity FROM SalesReturn sr INNER JOIN SalesReturnItem sri ON sr.InvoiceNo = sri.InvoiceNo INNER JOIN Staff s ON s.StaffId = sr.userID WHERE ReturnDate BETWEEN '" + startDate.ToString("yyyy-MM-dd") + "' AND '" + endDate.ToString("yyyy-MM-dd") + "' AND sr.InvoiceNo LIKE '%" + txtName.Text + "%' GROUP BY sr.InvoiceNo ORDER BY ReturnDate, sr.InvoiceNo DESC";
When I run that query I keep getting this error:
syntax error (missing operator) in query expression sri.invoiceNo =
sri.invoiceNo INNER JOIN Staff s oN s.Staffid = sr.userld
In Access you need parentheses when you have more than one join:
SELECT SalesReturnId, ReturnDate, sr.InvoiceNo,
(lastname & ', ' & firstname & ', ' & MI) as StaffName,
TotalAmount, SUM(sri.Quantity) as TotalQuantity
FROM (SalesReturn sr
INNER JOIN SalesReturnItem sri ON sr.InvoiceNo = sri.InvoiceNo )
INNER JOIN Staff s ON s.StaffId = sr.userID
WHERE ReturnDate BETWEEN '" + startDate.ToString("yyyy-MM-dd") + "' AND '" + endDate.ToString("yyyy-MM-dd") + "'
AND sr.InvoiceNo LIKE '%" + txtName.Text + "%'
GROUP BY sr.InvoiceNo
ORDER BY ReturnDate, sr.InvoiceNo DESC;

Converting query from where not exist to Left Join

i am using laravel 5.2 and i have an issue with converting this query
DB::select('SELECT m.*
FROM member_table AS m
JOIN treepaths AS t ON m.membershipid = t.descendant
WHERE m.stage >=' . $usermatrixtype . '
AND t.ancestor =' . "'" . $firstrightchildmembershipid . "'" . '
AND NOT EXISTS (
select user_id from matrix_users AS mr WHERE mr.user_id=m.membershipid AND matrix_id=' . $getusermatrix . '
) LIMIT ' . $results
);
Here is what i have tried
DB::select('SELECT m.membershipid
FROM member_table AS m
JOIN treepaths AS t ON m.membershipid = t.descendant
WHERE m.stage >=' . $usermatrixtype . '
AND t.ancestor =' . "'" . $firstleftchildmembershipid . "'" .'
LEFT OUTER JOIN matrix_users AS mu ON mu.user_id =m.membershipid WHERE
matrix_id=' . $getusermatrix . ' AND
mu.userid IS NULL
LIMIT ' . $results);
it returns nothing
try this:
DB::select('SELECT m.membershipid
FROM member_table AS m
JOIN treepaths AS t ON m.membershipid = t.descendant
LEFT JOIN matrix_users AS mu ON mu.user_id =m.membershipid AND matrix_id=' . $getusermatrix . '
WHERE mu.userid IS NULL AND m.stage >=' . $usermatrixtype . '
AND t.ancestor =' . "'" . $firstleftchildmembershipid . "'" .'
LIMIT ' . $results);

Getting joining tables not working codeigniter

When I use the $this->db->where() & $this->db->join() it is not retrieving any data. When var dump says NULL.
On my old code it works fine, using JOIN in query. Cannot work out what I am missing.
OLD Code Works Fine
public function get_admin_category($category_id) {
$language_id = $this->check_language();
$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . $this->db->dbprefix . "category_path cp LEFT JOIN " . $this->db->dbprefix . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$language_id . "' GROUP BY cp.category_id) AS path, (SELECT DISTINCT keyword FROM " . $this->db->dbprefix . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword FROM " . $this->db->dbprefix . "category c LEFT JOIN " . $this->db->dbprefix . "category_description cd2 ON (c.category_id = cd2.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd2.language_id = '" . (int)$language_id . "'");
return $query->row_array();
}
New Code What I Have Tried.
public function get_admin_category($category_id) {
$language_id = $this->check_language();
$this->db->select('GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR " > ")');
$this->db->distinct();
$this->db->from($this->db->dbprefix . 'category_path cp', 'LEFT');
$this->db->join($this->db->dbprefix . 'category_description cd1', 'cp.path_id = cd1.category_id AND cp.category_id != cp.path_id', 'LEFT');
$this->db->where('cp.category_id', 'c.category_id');
$this->db->where('cd1.language_id', $language_id);
$query = $this->db->get();
return $query->row_array();
}
How can I convert my old $this->db->query to codeigniter work with the join. http://www.codeigniter.com/userguide2/database/active_record.html
Try this it'll work for you.
$this->db->select("DISTINCT *,(SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . $this->db->dbprefix . "category_path cp LEFT JOIN " . $this->db->dbprefix . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$language_id . "' GROUP BY cp.category_id) AS path, (SELECT DISTINCT keyword FROM " . $this->db->dbprefix . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword", false);
$this->db->from($this->db->dbprefix . "category c");
$this->db->join($this->db->dbprefix . "category_description cd2", "c.category_id = cd2.category_id", "left");
$this->db->where("c.category_id", intval($category_id));
$this->db->where("cd2.language_id", intval($language_id));
$result = $this->db->get()->row_array();
return $result;
try this:
$this->db->select('DISTINCT *');
$this->db->select("(SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . $this->db->dbprefix . "category_path cp LEFT JOIN " . $this->db->dbprefix . "category_description cd1 ON (cp.path_id = cd1.category_id AND cp.category_id != cp.path_id) WHERE cp.category_id = c.category_id AND cd1.language_id = '" . (int)$language_id . "' GROUP BY cp.category_id) AS path", FALSE);
$this->db->select("(SELECT DISTINCT keyword FROM " . $this->db->dbprefix . "url_alias WHERE query = 'category_id=" . (int)$category_id . "') AS keyword", FALSE);
$this->db->from($this->db->dbprefix . "category c");
$this->db->join($this->db->dbprefix . "category_description cd2", "c.category_id = cd2.category_id", "left");
$this->db->where("c.category_id", intval($category_id));
$this->db->where("cd2.language_id", intval($language_id));

Building a wordpress query to select POSTS by category as well as a numeric range of a meta_key

This is a real estate Wordpress blog.
I'm working on a chained 3-select box.
I have most of it figured out, what I cannot figure out how to do is to query the posts to return only the meta_data of # of bedrooms based on Category and a price range.
I have it working to select based on category:
SELECT DISTINCT meta_value
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON
(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1
AND wp_term_taxonomy.term_id IN ($_POST[id])
AND meta_key = '_lwi_bedroomsCount'
GROUP BY wp_posts.ID
ORDER BY meta_value ASC
What I need to add to this query is a price range of these args:
$args['meta_query'][] = array(
'key' => '_lwi_price',
'value' => array(strval($pricerange[0]),strval($pricerange[1])),
'compare' => 'BETWEEN',
'type' => 'numeric', );
How can I add this to my SQL statement?
Note: $_POST[id] is the category ID.
### get term id by Category name ####
$sql = "SELECT * "
." FROM wp_b9la0x2_terms wp_b9la0x2_terms "
." WHERE (wp_b9la0x2_terms.name = '".$_REQUEST['category_name']."');";
$result = $conn->query($sql) or die($conn->error);
$row = $result->fetch_assoc();
#### get all post by category and also sub category ###
$sql = "SELECT wp_b9la0x2_posts.ID,"
. " wp_b9la0x2_posts.post_title, "
. " wp_b9la0x2_posts.post_content, "
. " wp_b9la0x2_posts.post_excerpt, "
. " wp_b9la0x2_posts.post_date, "
. " wp_b9la0x2_posts.post_date_gmt, "
. " wp_b9la0x2_posts.post_modified, "
. " wp_b9la0x2_posts.post_modified_gmt, "
. " wp_b9la0x2_posts.guid, "
. " wp_b9la0x2_term_taxonomy.description, "
. " wp_b9la0x2_terms.name, "
. " wp_b9la0x2_posts.post_author, "
. " wp_b9la0x2_users.display_name as author_name "
. " FROM wp_b9la0x2_terms "
. " LEFT JOIN wp_b9la0x2_term_taxonomy wp_b9la0x2_term_taxonomy "
." ON wp_b9la0x2_terms.term_id = wp_b9la0x2_term_taxonomy.term_id "
." LEFT JOIN wp_b9la0x2_term_relationships wp_b9la0x2_term_relationships "
." ON wp_b9la0x2_term_taxonomy.term_taxonomy_id = wp_b9la0x2_term_relationships.term_taxonomy_id "
." LEFT JOIN wp_b9la0x2_posts wp_b9la0x2_posts"
." ON wp_b9la0x2_term_relationships.object_id = wp_b9la0x2_posts.ID "
." LEFT JOIN wp_b9la0x2_users wp_b9la0x2_users"
." ON wp_b9la0x2_users.ID = wp_b9la0x2_posts.post_author "
." WHERE (wp_b9la0x2_term_taxonomy.term_id = '".$row['term_id']."' OR `parent` = '".$row['term_id']."')"
." AND (wp_b9la0x2_posts.post_status = 'publish')"
." AND (wp_b9la0x2_posts.post_type = 'post')"
. " GROUP BY wp_b9la0x2_posts.ID"
. " ORDER BY wp_b9la0x2_posts.post_date DESC";
$result = $conn->query($sql) or die($conn->error);
$res = array();
while($row = $result->fetch_assoc()) {
$test = array();
foreach($row as $key => $val){
$test[$key] = utf8_encode($val);
}
$res[] = $test;
}
echo json_encode($res);