Concatenated column to be store in two different columns - mysql

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.

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!

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

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?

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

Wordpress custom wp_users, wp_usermeta join not ordering properly

I have a custom query to pull users with their user_meta for a statistics plugin I wrote. I am able to select (WHERE) based on a date range, as well as ORDER BY any of the meta fields, however, I cannot do both at the same time. I am losing my mind!! Here is my query (with default variables for reference underneath) ...
$getUserData = $wpdb->get_results("SELECT
ID, user_email, user_registered,
first_name.meta_value as first_name,
last_name.meta_value as last_name,
telephone.meta_value as telephone,
country.meta_value as country,
company.meta_value as company,
address.meta_value as address,
city.meta_value as city,
professional_title.meta_value as professional_title,
state.meta_value as state,
areas_of_interest.meta_value as areas_of_interest
FROM wp_users
LEFT JOIN wp_usermeta AS first_name ON first_name.user_id=ID
AND first_name.meta_key='first_name'
LEFT JOIN wp_usermeta AS last_name ON last_name.user_id=ID
AND last_name.meta_key='last_name'
LEFT JOIN wp_usermeta AS telephone ON telephone.user_id=ID
AND telephone.meta_key='telephone'
LEFT JOIN wp_usermeta AS country ON country.user_id=ID
AND country.meta_key='country'
LEFT JOIN wp_usermeta AS company ON company.user_id=ID
AND company.meta_key='company'
LEFT JOIN wp_usermeta AS address ON address.user_id=ID
AND address.meta_key='address'
LEFT JOIN wp_usermeta AS city ON city.user_id=ID
AND city.meta_key='city'
LEFT JOIN wp_usermeta AS professional_title ON professional_title.user_id=ID
AND professional_title.meta_key='professional_title'
LEFT JOIN wp_usermeta AS state ON state.user_id=ID
AND state.meta_key='state'
LEFT JOIN wp_usermeta AS areas_of_interest ON areas_of_interest.user_id=ID
AND areas_of_interest.meta_key='areas_of_interest'
" . $where . "
ORDER BY " . $orderBy . " " . $sortOrder . " LIMIT 0, " . $page_limit);
$where = "WHERE (user_registered >= '" . $_REQUEST['from'] . " 00:00:00' and user_registered <= '" . $_REQUEST['to'] . " 00:00:00') ";
$orderBy = "user_registered";
$sortOrder = "ASC";
hint - mysql_error() telling us that if you place your variables correct you still forgeting to set there $page_limit