include wholesale field in mysql query - mysql

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!

Related

union with group by mysql

Hi I have a query as follows but group by is not working.
SELECT messages.thread_id,messages.subject,messages.message_id,messages.status,messages.attachment,login.name, DATE_FORMAT( messages.date, '%D-%b-%Y' ) as date from messages inner join login on messages.receiver = login.id where (messages.sender = '" . mysql_real_escape_string(trim($_SESSION['login_id'])) . "' and messages.status = 'trash' )
UNION SELECT messages.thread_id,messages.subject,messages.message_id,messages.status,messages.attachment,login.name, DATE_FORMAT( messages.date, '%D-%b-%Y' ) as date from messages inner join login on messages.sender = login.id where (messages.receiver = '" . mysql_real_escape_string(trim($_SESSION['login_id'])) . "' and messages.status = 'trash') GROUP BY messages.thread_id
Wrap your query to an outer query so that it can group the combined results from both the queries.
SELECT z.*
FROM
(
SELECT messages.thread_id, messages.subject, messages.message_id, messages.status,
messages.attachment, login.name, DATE_FORMAT( messages.date, '%D-%b-%Y' ) as date
FROM messages
INNER JOIN login ON messages.receiver = login.id
WHERE messages.sender = '" . mysql_real_escape_string(trim($_SESSION['login_id'])) . "'
AND messages.status = 'trash'
UNION
SELECT messages.thread_id, messages.subject, messages.message_id, messages.status,
messages.attachment, login.name, DATE_FORMAT( messages.date, '%D-%b-%Y' ) as date
FROM messages
INNER JOIN login ON messages.sender = login.id
WHERE messages.receiver = '" . mysql_real_escape_string(trim($_SESSION['login_id'])) . "'
AND messages.status = 'trash'
) AS z
GROUP BY z.thread_id
You have to put brackets around the union select, give it an alias and select from it:
SELECT A.* FROM
(messages.thread_id,messages.subject,messages.message_id,messages.status,mess
ages.attachment,login.name, DATE_FORMAT( messages.date, '%D-%b-%Y' ) as
date from messages inner join login on messages.receiver = login.id
where (messages.sender = '" .
mysql_real_escape_string(trim($_SESSION['login_id'])) . "' and
messages.status = 'trash' )
UNION SELECT
messages.thread_id,messages.subject,messages.message_id,messages.status,messa
ges.attachment,login.name, DATE_FORMAT( messages.date, '%D-%b-%Y' ) as
date from messages inner join login on messages.sender = login.id where
(messages.receiver = '" .
mysql_real_escape_string(trim($_SESSION['login_id'])) . "' and
messages.status = 'trash')) AS A
GROUP BY A.thread_id

Avoid duplicate rows when join multiple tables

I have problem with 3 tables: first (User) contains car dealers, second and third (Order_new, Car_demo) contains two types of cars sold by dealers. I need to sum number of sales and profit for a dealer.
Unfortunately it doesn't work correctly. One of dealer sold 11 cars from Order_new and 1 from Car_demo, but when I using COUNT a get 11 from Order_new (good) and 11 from Car_demo (not good...). The same with SUM - from Car_demo I get profit x 11.
This is important fragment of code:
SELECT
d.name AS name,
COUNT(cn_renault.id) AS ren_p,
COUNT(cd_renault.id) AS ren_demo_p,
SUM(cn_renault.profit) AS cn_profit,
SUM(cd_renault.profit) AS cd_profit,
FROM User d
LEFT OUTER JOIN (
SELECT DISTINCT
cd2.id AS id,
cd2.dealer AS sale_by,
cd2.price AS price,
FROM Order_new cd2
LEFT OUTER JOIN Car car ON car.id = cd2.car_brand
WHERE DATE(cd2.' . $by . ') >= \'' . $date . '\' AND DATE(cd2.' . $by . ') < \'' . $date_end . '\' AND car.brand = \'Renault\'
GROUP BY cd2.id
) cn_renault ON cn_renault.sale_by = d.id
LEFT OUTER JOIN (
SELECT DISTINCT
cd2.id AS id,
cd2.sale_by AS sale_by,
cd2.selling_price AS price,
FROM Car_demo cd2
LEFT OUTER JOIN Car car ON car.id = cd2.car_brand
WHERE DATE(cd2.' . $by_d . ') >= \'' . $date . '\' AND DATE(cd2.' . $by_d . ') < \'' . $date_end . '\' AND cd2.sale_status >= 2 AND car.brand = \'Renault\'
GROUP BY cd2.id
) cd_renault ON cd_renault.sale_by = d.id
WHERE d.id = ' . $dealer_id . '
GROUP BY d.id
I know I can do it this way:
LEFT OUTER JOIN (
SELECT DISTINCT
COUNT(cd2.id) AS number_of,
SUM(cd2.price) AS price,
FROM Order_new cd2
LEFT OUTER JOIN Car car ON car.id = cd2.car_brand
WHERE DATE(cd2.' . $by . ') >= \'' . $date . '\' AND DATE(cd2.' . $by . ') < \'' . $date_end . '\' AND car.brand = \'Renault\'
GROUP BY cd2.dealer
) cn_renault ON cn_renault.sale_by = d.id
and it works, but I need cars id to join another tables in query...
Any ideas?

Concatenated column to be store in two different columns

I have a website and when customer register on it the data is stored in the admin panel, Now If you see in the below code at the first line emp_name and emp_ID is saving in a single column, and I want to save it in two different columns how to differentiate it.
$sql = "SELECT c.customer_id, CONCAT(c.emp_name, ' ', c.emp_ID) AS name,
c.email, c.mobile_no, CONCAT(oca.address_1,oca.address_2) AS address,
oca.city, oca.postcode, occ.name as Country, c.ip,
IF( c.status = 1, 'Enabled','Disabled' ) AS status,
IF( c.approved = 1, 'Yes', 'No' ) AS approved, c.date_added, cgd.name AS customer_group FROM " . DB_PREFIX . "customer c
LEFT JOIN " . DB_PREFIX . "customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id)
LEFT JOIN " . DB_PREFIX . "address oca ON (c.address_id=oca.address_id)
LEFT JOIN " . DB_PREFIX . "country occ ON (oca.country_id=occ.country_id)
WHERE cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
Try this:
SELECT c.customer_id,
c.emp_id,
c.emp_name AS NAME,
c.email,
c.mobile_no,
Concat(oca.address_1,oca.address_2) AS address,
oca.city,
oca.postcode,
occ.NAME AS Country,
c.ip,IF( c.status = 1, 'Enabled', 'Disabled' ) as status,
IF( c.approved = 1, 'Yes', 'No' ) AS approved, c.date_added, cgd.NAME AS customer_group FROM " . DB_PREFIX . "customer c LEFT JOIN " . DB_PREFIX . "customer_group_description cgd ON (
c.customer_group_id = cgd.customer_group_id
)
LEFT JOIN " . DB_PREFIX . "address oca ON (
c.address_id=oca.address_id
)
LEFT JOIN " . DB_PREFIX . "country occ ON (
oca.country_id=occ.country_id
)
WHERE cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'
MySQL CONCAT() function is used to concatenate two or more strings to form a single string. This is why you see the data in a single column. Simply remove the function, if you want to show the data in separate columns.

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

sql query show error as near order by

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