I've got the following query:
SELECT t1.shop_id, t1.cat_id, t1.detailtype_id, t1.exact_matching, COUNT(t3.part) AS AantalOnderdelen
FROM tbldetailspershop t1
LEFT JOIN tbltranslationscompound t2 ON t1.compound_group_id = t2.group_id AND t2.part_detailtype_id=825
LEFT JOIN tbltranslationscompound t3 ON t1.compound_group_id = t3.group_id
WHERE t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0 #and t2.part_detailtype_id=825
GROUP BY t1.shop_id, t1.cat_id, t1.detailtype_id
HAVING COUNT(t3.part)=1
Now, this results in 3000 rows which have t1.exact_matching 0. See below for the output of the select query (just 1 row)
shop_id;cat_id;detailtype_id;exact_matching
6;11111;14519;0
Now I'd like to update the exact_matching column to 1 for all the rows returned by the select query from above. I can't figure out how to write the update statement though, as I want to make sure only these 3000 rows are affected which meet the having and where conditions from the above select query.
I got this now:
UPDATE tbldetailspershop SET exact_matching = 1
WHERE (shop_id, cat_id, detailtype_id) IN
(
SELECT t1.shop_id, t1.cat_id, t1.detailtype_id
FROM tbldetailspershop t1
LEFT JOIN tbltranslationscompound t2 ON t1.compound_group_id = t2.group_id AND t2.part_detailtype_id=825
LEFT JOIN tbltranslationscompound t3 ON t1.compound_group_id = t3.group_id
WHERE t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0
GROUP BY t1.shop_id, t1.cat_id, t1.detailtype_id
HAVING COUNT(t3.part)=1
)
update shopdetails
set exact_matching = 1
where (shop_id, cat_id, detailtype_id, exact_matching) in
(
select t1.shop_id, t1.cat_id, t1.detailtype_id, t1.exact_matching from shopdetails t1
left join transcomp t2 on t1.compound_group_id = t2.group_id and t2.part_detailtype_id=825
left join transcompt3 on t1.compound_group_id = t3.group_id
where t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0
group by t1.shop_id, t1.cat_id, t1.detailtype_id
having count(t3.part)=1
)
Related
If someone can help with this MySQL query that would be great
I want to update this table
UPDATE catalog_product_entity_int
This query brings an array of values and works.
select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
GROUP BY catalog_product_relation.parent_id;
This is what I want to set based on the above query
SET catalog_product_entity_int.value = 1
where catalog_product_entity_int.attribute_id ='579'
and catalog_product_relation.parent_id = catalog_product_entity_int.entity_id
I'm not sure if I need to get the select query into a temp table first and if so how?
Theeasiest way is to join both table
i added aliases for he table to make it more readable
UPDATE catalog_product_entity_int t1,(select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
GROUP BY catalog_product_relation.parent_id) t2
SET t1.value = 1
where t1.attribute_id ='579'
and t2.parent_id = t1.entity_id
catalog_product_entity.sku and catalog_product_flat_1.special_price aren't used in the update and so you should check if they are needed
Try the select query
SELECT t1.value, t1.entity_id
FROM
catalog_product_entity_int t1 INNER JOIN (select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
GROUP BY catalog_product_relation.parent_id) t2
ON t2.parent_id = t1.entity_id
where t1.attribute_id ='579'
Thank you for the help. In the end this is what i did to make it work.
First create a temp table to store the values of the query
CREATE TABLE on_sale_updating AS
select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
and catalog_product_flat_1.special_to_date IS NULL OR catalog_product_flat_1.special_to_date >= CURDATE()
GROUP BY catalog_product_relation.parent_id;
Then run an update query against the temp table
update catalog_product_entity_int
inner join on_sale_updating ON catalog_product_entity_int.entity_id = on_sale_updating.parent_id
SET catalog_product_entity_int.value = 1
where catalog_product_entity_int.attribute_id ='579'
Once run for the first time add
DROP TABLE on_sale_updating;
to the first part.
All, I face a problem with join mysql. I want to get a result with count a unique amount of staff_id field using GROUP BY. If I run this query I got result as I want at one step.
SELECT id,staff_id,note,warning_date FROM tbl_warning GROUP BY staff_id HAVING (count(staff_id) > 0);
The next I want to join two more tables to get field such as tbl_employment.com_id as comid, tbl_staff.name, tbl_staff.gender but the result are duplicate.
`SELECT` `tbl_warning`.`id`, `tbl_warning`.`staff_id`, `tbl_warning`.`note`, `tbl_warning`.`warning_date`,`tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`, `tbl_staff`.`gender` FROM `tbl_warning`
JOIN `tbl_employment` ON `tbl_employment`.`staff_id` = `tbl_warning`.`staff_id`
JOIN `tbl_staff` ON `tbl_staff`.`id` = `tbl_warning`.`staff_id`
HAVING (SELECT `staff_id` FROM `tbl_warning` GROUP BY `staff_id` HAVING (count(staff_id) > 1));
I want unique result same first screenshot.
Thank you!
You should join to a subquery which finds the matching staff by count:
SELECT
t1.id,
t1.staff_id,
t1.note,
t1.warning_date,
t2.com_id as `comid`,
t3.name,
t3.gender
FROM tbl_warning t1
INNER JOIN tbl_employment t2
ON t2.staff_id = t1.staff_id
INNER JOIN tbl_staff t3
ON t3.id = t1.staff_id
INNER JOIN
(
SELECT staff_id
FROM tbl_warning
GROUP BY staff_id
HAVING COUNT(*) > 1
) t
ON t.staff_id = t2.staff_id;
I have the following query that works great when all of the INNER JOIN criteria is met. Unfortunately, this query returns zero results when for example INNER JOIN TableSample6 ON TableSample1.Field4 = TableSample6.Field6 is not found.
Can I restructure this query to return results even if some of the INNER JOIN values do not have a match in some cases?
SELECT DISTINCT
TableSample1.Field4,
TableSample.Field2,
TableSample2.Field2,
TableSample3.Field3,
TableSample4.Field4,
TableSample5.Field1,
TableSample.Field3,
TableSample6.Field1,
TableSample6.Field2,
TableSample7.Field7,
TableSample3.Field4,
TableSample4.Field2,
TableSample5.Field4
FROM TableSample1
INNER JOIN TableSample2 ON TableSample1.Field2 = TableSample2.Field4
INNER JOIN TableSample ON TableSample1.Field6 = TableSample.Field4
INNER JOIN TableSample3 ON TableSample1.Field3 = TableSample3.Field2
INNER JOIN TableSample4 ON TableSample1.Field7 = TableSample4.Field3
INNER JOIN TableSample5 ON TableSample1.Field8 = TableSample5.Field2
INNER JOIN TableSample6 ON TableSample1.Field4 = TableSample6.Field6
WHERE (((TableSample1.Field4)="xxxxxx" AND (TableSample.Field2)="xxxxxx"))
ORDER BY TableSample1.Field2;
You could use LEFT JOIN:
SELECT DISTINCT
t1.Field4,
t.Field2,
t2.Field2,
t3.Field3,
t4.Field4,
t5.Field1,
t.Field3,
t6.Field1,
t6.Field2,
t7.Field7, -- PROBLEM: you never join to TableSample7
t3.Field4,
t4.Field2,
t5.Field4
FROM TableSample1 t1
LEFT JOIN TableSample2 t2
ON t1.Field2 = t2.Field4
LEFT JOIN TableSample t
ON t1.Field6 = t.Field4
LEFT JOIN TableSample3 t3
ON t1.Field3 = t3.Field2
LEFT JOIN TableSample4 t4
ON t1.Field7 = t4.Field3
LEFT JOIN TableSample5 t5
ON t1.Field8 = t5.Field2
LEFT JOIN TableSample6 t6
ON t1.Field4 = t6.Field6
WHERE t1.Field4 = "xxxxxx" AND
t.Field2 = "xxxxxx"
ORDER BY t1.Field2;
You could also use COALESCE() to handle records which do not match to all of the tables. For example, if TableSample2.Field2 were varchar, you could use COALESCE(t2.Field2, 'NA') to show NA instead of NULL.
I have 2 SQL joins here, one returns good results, the other returns 0 result set. Why is this? They are very similar except for join/column/table order. (By the way I'm using MySQL here)
Working Join (returns over 240,000 rows of good data)
SELECT
t1.email AS "email",
t1.record_id AS "record_id",
t2.status AS "status",
t3.item_record_id AS "item_record_id",
t1.ordered_at AS "started_at"
FROM `rs_orders` t1
INNER JOIN `cb_all_transactions` t2
ON t1.record_id = CONCAT('cb_sl_', t2.receipt)
INNER JOIN `rs_order_items` t3
ON t1.record_id = t3.order_record_id
WHERE t2.recurring = 'true'
ORDER BY t1.ordered_at ASC
NON Working Join (returns 0 rows)
SELECT
t1.email AS "email",
t1.record_id AS "record_id",
t1.ordered_at AS "started_at",
t2.item_record_id AS "item_record_id",
t3.status AS "status"
FROM rs_orders t1
INNER JOIN rs_order_items t2
ON t1.record_id = t2.order_record_id
INNER JOIN cb_all_transactions t3
ON t2.order_record_id = CONCAT('cb_sl_' + t3.receipt)
WHERE t3.recurring = 'true'
They're not the same. The second has CONCAT('cb_sl_' + t3.receipt). Replace + with ,.
How can I combine Join these two tables?
Table 1
SELECT job_category.JobCategoryId, job_category.JobCategoryName, count(job_position.JobCategoryId)
AS AvailableCategories
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName
Table 2
(SELECT job_category.JobCategoryId, job_category.JobCategoryName, count(job_position.ContactId) AS AllocatedJobs
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
WHERE job_position.ContactId > 0
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName)
Thanks
I think it should be like this
Select T1.JobCategoryId, T1.JobCategoryName, T1.AvailableCategories, T2.AllocatedJobs
FROM
(SELECT job_category.JobCategoryId as JobCategoryId, job_category.JobCategoryName as JobCategoryName, count(job_position.JobCategoryId)
AS AvailableCategories
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName) as T1
INNER JOIN
((SELECT job_category.JobCategoryId as T2JobCategoryId, job_category.JobCategoryName as T2JobCategoryName, count(job_position.ContactId) AS AllocatedJobs
FROM job_position
Right Outer JOIN job_category ON job_position.JobCategoryId = job_category.JobCategoryId
WHERE job_position.ContactId > 0
GROUP BY job_category.JobCategoryId, job_category.JobCategoryName)) as T2
ON T1.JobCategoryId = T2.T2JobCategoryId