Good Day! All Fridays,
I have some problem in my sql query. I'm using IN class with subquery like this
SELECT
cm.category_id,
cd.name
FROM
category_master cm,
category_detail cd,
brand_to_categories b2c
WHERE
cm.category_id = b2c.category_id
AND
cd.category_id = cm.category_id
AND
cd.language_id = 1
AND
cm.status <> 2
AND
cm.category_id IN (SELECT DISTINCT sub_dd.categories FROM distribution_master bdm, distribution_detail bdd, subscription_category_to_brand_user sub_dd WHERE bdd.distribution_id = bdm.distribution_id AND bdm.distributor_id = 35 AND bdd.brand_id = 7191 AND sub_dd.sub_d_id = bdd.id)
AND
b2c.brand_id = 7191;
The following is the sub-query which is creating problem for me.
cm.category_id IN (
SELECT DISTINCT
sub_dd.categories
FROM
distribution_master bdm,
distribution_detail bdd,
subscription_category_to_brand_user sub_dd
WHERE
bdd.distribution_id = bdm.distribution_id
AND
bdm.distributor_id = 35
AND
bdd.brand_id = 7191
AND
sub_dd.sub_d_id = bdd.id)
the result of the sub-query is like this.
3913,4517,6059,7137,7138,7139,7140,7141,7144
this result is coming from only single row in the target table because I stored these ids as string in the filed.
Now the problem is this, I can not get results of the all categories. Main query final result only return one category information which category_id is 3913. But if I run this query manually with sub-query values instead of the sub-query then it returns all the categories results.
Manual query with sub-query values is like this
SELECT
cm.category_id,
cd.name
FROM
category_master cm,
category_detail cd,
brand_to_categories b2c
WHERE
cm.category_id = b2c.category_id
AND
cd.category_id = cm.category_id
AND
cd.language_id = 1
AND
cm.status <> 2
AND
cm.category_id IN (3913,4517,6059,7137,7138,7139,7140,7141,7144)
AND
b2c.brand_id = 7191;
Please help me regarding this problem.
Sorry I forget, I'm using Mysql
Assuming you are using MySQL, use FIND_IN_SET:
WHERE
...
FIND_IN_SET(cm.category_id,
(SELECT DISTINCT sub_dd.categories
FROM distribution_master bdm,
distribution_detail bdd,
subscription_category_to_brand_user sub_dd
WHERE bdd.distribution_id = bdm.distribution_id AND
bdm.distributor_id = 35 AND
bdd.brand_id = 7191 AND
sub_dd.sub_d_id = bdd.id)) > 0
If you are using SQL Server, then we have to do a bit more work:
WHERE ',' + (SELECT DISTINCT ...) + ',' LIKE '%,' + cm.category_id + ',%'
General comment: Avoid storing CSV data in your SQL tables. MySQL almost made the problem worse by offering FIND_IN_SET and making it easier to skirt good table design.
My SQL query is:
UPDATE REGISTRUDENT set qual=1
WHERE studentolevelsubjects AS sls
INNER JOIN courserequirements AS csreq ON sls.subject_id = csreq.subject_id
WHERE sls.stud_id = '$stud_id' AND sls.grade_id>=csreq.min_grade AND sls.examno = '$examno' AND csreq.course_id = '$course_id'
I'm having issue comparing two different table to get if a registrant qualified based on the course requirement I need help
course requirement table
The subject passed by the student in exams
I intend updating and setting the qual =1 if the student qualifies
Many Thanks
Your query syntax is not correct. It rather should be
UPDATE REGISTRUDENT rs
JOIN studentolevelsubjects sls ON sls.some_column = rs.some_column //missing this JOIN
JOIN courserequirements csreq ON sls.subject_id = csreq.subject_id
AND sls.grade_id >= csreq.min_grade
WHERE sls.stud_id = '$stud_id'
AND sls.examno = '$examno'
AND csreq.course_id = '$course_id'
SET rs.qual = 1;
This is my query:
SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190
It runs perfectly on MySQL directly but when I run it from my PHP codeigniter model I get the #1054 error. I have no idea why this is happening. Please help.
Here is the PHP code:
$query = $this->db->query("SELECT COUNT( DISTINCT (paypal_transaction.buyerId) ) AS cid
FROM eg_posts_details
INNER JOIN paypal_transaction ON paypal_transaction.id = eg_posts_details.OrderId
WHERE seller_id =190");
As per your image reference, paypal transaction table contain buyerId and you used it as buyer_id. So use the following.
Use like this
$sql = "select count(distinct(`paypal_transaction`.`buyerId`)) as `cid` from `eg_posts_details` inner join `paypal_transaction` on `paypal_transaction`.`id` = `eg_posts_details`.`OrderId` where `seller_id`= '190' ";
$query = $this->db->query($sql);
Hope its work for you
UPDATE student
INNER JOIN
fee_head2 ON (student.new_old = fee_head2.new_old)
INNER JOIN
fee_head2 ON (student.class = fee_head2.class)
SET
student.head1_apr = fee_head2.head1_apr
This is showing #1066 - Not unique table/alias: 'fee_head2'.
Can anyone correct this query for me?
No need to JOIN the same table again, try this:
UPDATE student INNER JOIN fee_head2
ON (student.new_old = fee_head2.new_old)
AND (student.class = fee_head2.class)
SET student.head1_apr = fee_head2.head1_apr
Aziz beat me to the obvious naswer.
UPDATE student SET student.head1_apr =
CASE
WHEN student.new_old = fee_head2.new_old AND student.class = fee_head2.class THEN fee_head2.head1_apr
ELSE NULL
END
I have updated an old system and I have a query problem.
To get to business this is the direct problem:
There are new records witch are associated with an id to other records. There are also old records which are not.
Case1 : there is a INT id and when that is present the query has to use AND after an include.
Case 2 : when the value of the INT id is 0 it has to do nothing
This is the part of the query where I need to make an variable AND statement:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id AND i.id = v.inkoop_id
I never used IF stamens inside a query but i am looking for something like this:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id if(v.inkoop_id > 0){AND i.id = v.inkoop_id}
Try this:
LEFT JOIN table v
ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND (v.inkoop_id <= 0 OR i.id = v.inkoop_id)
How about this....
LEFT JOIN table v ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND ((v.inkoop_id > 0 AND i.id = v.inkoop_id) OR (v.inkoop_id = 0))