codigniter join query issue with 0 result - php

working in a section to get average rating for deals in same query and these are the details,
avg(rv.ratevalue) as avRatings
$this->db->join('product_rating rv','rv.productid = b.dealId','left');
$this->db->where('b.categoryId',$categoryId);
Which is returning 0 result which expected to be 4 . If i remove the category condition will return 4 insted of 0. Unable to detect the issue.
The full query is -
SELECT `b`.`deal_available_city`, `b`.`shippingType`, `b`.`priceType`, `b`.`dealId`, `b`.`dealUrl`, `b`.`dealTitle`, `b`.`slug`, `b`.`dealDetails`, `b`.`extraDetails`, `b`.`isHomeScreenBigDeal`, `b`.`deal_available_city`, `b`.`aditionalDetails`, `b`.`status`, `b`.`dateAdded`, `b`.`categoryId`, `b`.`siteId`, `b`.`isPinned`, `b`.`priceId`, `b`.`price`, `b`.`startDate`, `b`.`endDate`, `b`.`NumberOfClicked`, `b`.`discountPrice`, `b`.`discountPercentage`, `b`.`dealBrandId`, `b`.`endTime`, `b`.`startTime`, `b`.`howtousethisoffer`, `b`.`deal_location`, `b`.`cancellationpolicy`, `b`.`dealType`, `b`.`totalavailabledeals`, `b`.`numberofdealused`, `b`.`addedTime`, `i`.`thumbImage`, `i`.`imageUrl`, `i`.`normalimageurl`, `s`.`SiteUrl`, `s`.`SiteName`, `s`.`Site_alias`, `s`.`SiteLogo`, `c`.`cat_name`, `c`.`cat_color`, `c`.`cat_alias`, `p`.`price_name`, `p`.`price_symbol`, `p`.`price_shortcode`, `st`.`shId`, `st`.`shipping_text`, `l`.`user_id` as `isMarkedFavourite`, `dt`.`user_id` as `islikedorDisliked`, avg(rv.userid) as avRatings FROM `cob_dealdetails` as `b` LEFT JOIN `cob_dealImages` `i` ON `i`.`dealId` = `b`.`dealId` LEFT JOIN `cob_brand` `s` ON `s`.`SiteId` = `b`.`siteId` LEFT JOIN `cob_shipping_details` `st` ON `st`.`shId` = `b`.`shippingType` LEFT JOIN `cob_price_type` `p` ON `p`.`id` = `b`.`priceId` LEFT JOIN `cob_coupon_category` `c` ON `c`.`catId` = `b`.`categoryId` LEFT JOIN `cob_dealfavorite` `l` ON `l`.`deal_id` = `b`.`dealId` AND `l`.`user_id` = 3259 LEFT JOIN `cob_deallike` `dt` ON `dt`.`deal_id` = `b`.`dealId` AND `dt`.`user_id` = 3259 LEFT JOIN `cob_product_rating` `rv` ON `rv`.`productid` = `b`.`dealId` WHERE `b`.`showinhomescreen` = '1' AND `i`.`imgisdefault` = '1' AND `i`.`status` = '1' AND `i`.`imageOrder` = '1' AND FIND_IN_SET(1,b.deal_available_city) !=0 AND `b`.`startTime` <= '2017-07-22 19:00:09' AND `b`.`endTime` >= '2017-07-22 19:00:09' AND `b`.`categoryId` = '2' GROUP BY `b`.`dealId` ORDER BY `b`.`dealId` DESC LIMIT 10

Related

mySql LEFT JOIN not returning data as expected when Condition by ID php variable

When I use the PHP $qs variable in the WHERE condition it works fine but in LEFT JOIN condition, it's not returning any data.
Problem Line: AND e.terms_id_user =
My Query: (Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = 1001
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'
My Query: (Not Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = '$qs'
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'

my query returns duplicates

Hello i have a query that selects products from a database, but for some reason it returns duplicates. The problem is in my joins i guess since in the product table there is no duplicate product. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.slug,
p.id_product,
p.price,
pig.image,
c1.slug as ssubcat,
c2.slug as subcat,
c3.slug as cat
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.slug = :slug OR c2.slug = :slug OR c3.slug = :slug )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.name ASC
LIMIT $start, $row_limit");

SELECT product variants SQL

This is my query without variant options
SELECT p.*, pd.`name` AS `product_name`
FROM `product` AS `p`
LEFT JOIN `product_description` AS `pd` ON p.`id` = pd.`product_id`
LEFT JOIN `product_to_variant` AS `pv` ON p.`id` = pv.`product_id`
WHERE p.`status` = 0
GROUP BY p.`id`
ORDER BY p.`id` DESC;
SQLFiddle: http://sqlfiddle.com/#!9/8955b/5
and the follwing query has variant options but it doesn't work
SELECT p.*, pd.`name` AS `product_name`
FROM `product` AS `p`
LEFT JOIN `product_description` AS `pd` ON p.`id` = pd.`product_id`
LEFT JOIN `product_to_variant` AS `pv` ON p.`id` = pv.`product_id`
WHERE p.`status` = 0
AND (pv.`feature_id` = 2 AND pv.`variant_id` = 6)
AND (pv.`feature_id` = 3 AND pv.`variant_id` = 11)
GROUP BY p.`id`
ORDER BY p.`id` DESC;
and I also trying to query but there is no output
SELECT pv.* FROM `product_to_variant` AS `pv`
WHERE (pv.`feature_id` = 2 AND pv.`variant_id` = 2)
AND (pv.`feature_id` = 3 AND pv.`variant_id` = 11)
Do you have any other idea how to receive the products: 14, 15 by specific variant_id 6 AND 11 http://prntscr.com/ect2oh
Here is one method to do what you want:
SELECT p.*, pd.name AS product_name
FROM product p LEFT JOIN
product_description pd
ON p.id = pd.product_id JOIN
product_to_variant pv
ON p.id = pv.product_id
WHERE p.status = 0 AND
((pv.feature_id = 2 AND pv.variant_id = 6) OR
(pv.feature_id = 3 AND pv.variant_id = 11)
)
GROUP BY p.id
HAVING COUNT(DISTINCT feature_id) = 2
ORDER BY p.id DESC;
Notes:
No row can meet your original conditions. Because a column cannot have two values at the same time. Hence the OR rather than AND.
The HAVING clause checks that both values match.
There is no need for a LEFT JOIN to pv, because you are checking values from that table in the WHERE clause -- there have to be matches.
The LEFT JOIN to pa is probably also unnecessary.
Using backticks everywhere just makes the query harder to write and to read.
Maybe like this:
SELECT pv.* FROM `product_to_variant` AS `pv`
WHERE (pv.`feature_id` = 2 AND pv.`variant_id` = 2)
OR (pv.`feature_id` = 3 AND pv.`variant_id` = 11)
Instead of AND, which says, you need both feature_id and variant_id in the results, use OR because it takes both.

The below query is executing perfectly on sqlyog but its not working with CI $this->db->query($sql)

Query:
SELECT em.`Full_Name`,em.`VEEVA_Employee_ID`,COUNT(dm.`Account_ID`) AS
No_of_Doctors ,COUNT(p.`Doctor_Id`)AS No_of_Doctors_profiled,rt.`target` AS
Target_New_Rxn_for_the_month,SUM(rp.`Planned_Rx`) AS
Planned_New_Rxn,COUNT(ap.`Act_Plan`) AS No_of_Doctors_planned,
COUNT(CASE WHEN ar.`Activity_Done`='Yes' THEN 1 END) AS checkk
FROM ( SELECT `VEEVA_Employee_ID`,`Full_Name` FROM Employee_Master
WHERE `Reporting_VEEVA_ID` = 'gi00145822') AS em
LEFT JOIN (SELECT d.`Account_ID`, ed.`VEEVA_Employee_ID` FROM Doctor_Master d
INNER JOIN Employee_Doc ed ON ed.`VEEVA_Account_ID` = d.`Account_ID` AND `ed`.`Status`='1'
WHERE `VEEVA_Employee_ID` IN (SELECT `VEEVA_Employee_ID` FROM Employee_Master WHERE `Reporting_VEEVA_ID` = 'gi00145822')
AND d.`Individual_Type` = 'Doctor' ) AS dm ON em.`VEEVA_Employee_ID` = dm.`VEEVA_Employee_ID`
LEFT JOIN (SELECT `Doctor_Id`, `VEEVA_Employee_ID` FROM Profiling WHERE `Product_Id` = 6 AND `STATUS` = 'Submitted' AND `Cycle` = 2 GROUP BY `Doctor_Id`, `VEEVA_Employee_ID`) AS
p ON em.`VEEVA_Employee_ID` = p.`VEEVA_Employee_ID` AND dm.`Account_ID` = p.`Doctor_Id`
LEFT JOIN Rx_Target rt ON em.`VEEVA_Employee_ID`=rt.`VEEVA_Employee_ID`AND rt.`Status`='Submitted' AND rt.`Product_Id`=6 AND rt.`Month`=12 AND rt.`Year`=2016
LEFT JOIN Rx_Planning rp ON dm.`Account_ID` = rp.`Doctor_Id` AND rp.`Approve_Status` ='Approved' AND rp.`Product_Id`=6 AND rp.`Month`=12 AND rp.`Year`=2016 AND rp.`VEEVA_Employee_ID` = em.`VEEVA_Employee_ID`
LEFT JOIN Activity_Planning ap ON dm.`Account_ID` = ap.`Doctor_Id` AND ap.`Approve_Status` = 'Approved' AND ap.`Product_Id`=6 AND ap.`Month`=12 AND ap.`Year`=2016 AND em.`VEEVA_Employee_ID` = ap.`VEEVA_Employee_ID`
LEFT JOIN Activity_Reporting ar ON dm.`Account_ID` = ar.`Doctor_Id` AND ar.`Approve_Status` = 'Approved' AND ar.`Product_Id`=6 AND ar.`Month`=12 AND ar.`Year`=2016 AND em.`VEEVA_Employee_ID` = ar.`VEEVA_Employee_ID`
GROUP BY em.`VEEVA_Employee_ID`
It executing perfectly on sqlyog but its not working with CI $this->db->query($sql)
Actually i got the solution.
its exits the current process in middle.
i allocate resources.
ini_set('max_execution_time',300);
and
ini_set('memory_limit','128M');
and works perfectly.
anyway thank you all.

How To Left Join A Table Based On The Value Received From Inner Join Of The Same Query

I am trying to run the following query, and I keep getting a syntax error. The query works fine without the LEFT JOIN. How can I implement the LEFT JOIN without error?
SELECT
m.mid,
m.seq,
m.created_on,
m.created_by,
m.body,
r.status,
u.username_clean
FROM message_recipient r
INNER JOIN message m
ON m.mid = r.mid AND m.seq = r.seq
WHERE r.uid = ".$logged_in_id."
AND r.status in ('A', 'N')
AND r.seq = (
SELECT
MAX(rr.seq)
FROM message_recipient rr
WHERE rr.mid = m.mid
AND rr.status in ('A', 'N')
)
AND IF (m.seq=1 and m.created_by = ".$logged_in_id." , 1=0, 1=1)
ORDER BY created_on DESC
LEFT JOIN users u
ON u.user_id = m.created_by
The LEFT JOIN is in the wrong place. All of the JOINs need to be before the WHERE clause:
SELECT
m.mid,
m.seq,
m.created_on,
m.created_by,
m.body,
r.status,
u.username_clean
FROM message_recipient r
INNER JOIN message m
ON m.mid = r.mid AND m.seq = r.seq
LEFT JOIN users u
ON u.user_id = m.created_by
WHERE r.uid = ".$logged_in_id."
AND r.status in ('A', 'N')
AND r.seq = (
SELECT
MAX(rr.seq)
FROM message_recipient rr
WHERE rr.mid = m.mid
AND rr.status in ('A', 'N')
)
AND IF (m.seq=1 and m.created_by = ".$logged_in_id." , 1=0, 1=1)
ORDER BY created_on DESC
The SQL clauses go in the following order:
SELECT
FROM
JOIN
WHERE
GROUP BY
ORDER BY
Even if you have multiple joins they will all appear before the WHERE

Categories