Fields lng and lat are decimal fields in each table.
update s
join t on s.id = t.id
set s.lat = t.lat , s.lng = t.lng
where s.id = t.id and (conditions)
In same select-query after running this update-query I not see difference - seem that UPDATE didn't run.
Someone have idea ?...
Try this, put conditions first and set after that
UPDATE s
JOIN t on s.id = t.id and (conditions)
SET s.lat = t.lat , s.lng = t.lng
Related
The query below does what I want but takes a long time to finish. I have been trying to write it with INNER JOIN instead but can not figure out how to do it.
UPDATE cf_ab_companies
SET cf_ab_companies.col_330 = (
SELECT aaa_items.amount
FROM aaa_items
WHERE aaa_items.customer = cf_ab_companies.model_id
AND aaa_items.sku = 10
);
cf_ab_companies.model_id is unique value in the table.
There can be several records where aa_items.customer = 314 , but aa_items.customer = 314 AND aa_item.sku = 10 may only occur once.
Hope you understand what I mean. Thanks for all help.
UPDATE Multiple-table syntax can be found https://dev.mysql.com/doc/refman/8.0/en/update.html
using this your query would look like
UPDATE cf_ab_companies join
aaa_items on aaa_items.customer = cf_ab_companies.model_id
SET cf_ab_companies.col_330 = aaa_items.amount
WHERE aaa_items.sku = 10
;
Here is the MySQL UPDATE ... JOIN ... SET syntax:
UPDATE cf_ab_companies c
INNER JOIN aaa_items i ON i.customer = c.model_id AND i.sku = 10
SET c.cf_ab_companies.col_330 = i.aaa_items.amount
update cf_ab_companies c
join aaa_items a on a.customer = c.model_id
set c.col_330 = a.amount
where a.sku = 10
......
I believe an UPDATE FROM can help:
UPDATE cf_ab_companies
SET cf_ab_companies.col_330 = aaa_items.amount
FROM cf_ab_companies INNER JOIN aaa_items
ON cf_ab_companies.model_id = aaa_items.customer AND aaa_items.sku=10
I am working on a project where I need to update 2 tables in a query.
I am doing this
UPDATE ward_beds a, ipd_patient_list b
SET
a.occupation_status = 'empty',
b.patient_status = 'Discharged'
WHERE
a.ward_id = b.ward_id AND b.patient_id = '4'
AND
b.appointment_id = '6' AND b.ward_id = '1'
so far it is working, now I want to update this
b.patient_status = 'Discharged'
on the row which is the last in all matching rows. I tried to put this
ORDER BY b.row_id DESC LIMIT 1
but it shows
#1221 - Incorrect usage of UPDATE and ORDER BY
error. How should I do it?
You can't use limit in this kind of update
but you could seach for the correct id using a subquery and join to the others tables
UPDATE ward_beds a
INNER JOIN (
select ward_id, max(row_id) last_id
from ipd_patient_list
group by ward_id
) t on t.ward_id = a.ward_id
INNER JOIN ipd_patient_list b ON a.ward_id = b.ward_id
AND b.patient_id = '4'
AND b.appointment_id = '6'
AND b.ward_id = '1'
AND b.row_id = t.last_id
SET a.occupation_status = 'empty',
b.patient_status = 'Discharged'
I want to do the query directly a Magento database in MySQL but it gives me duplicates. Could you please help me?
SELECT DISTINCT
`catalog_product_entity`.`sku`
, `catalog_product_flat_1`.`name`
, `catalog_product_entity_text`.`value` AS `description`
, `catalog_product_flat_1`.`url_key`
, `catalog_product_flat_1`.`small_image`
, `catalog_product_flat_1`.`price`
, `catalog_product_flat_1`.`special_price`
, `catalog_product_flat_1`.`designer_value`
, `catalog_product_flat_1`.`color_value`
FROM
`ac_magento_gold`.`catalog_product_flat_1`
INNER JOIN `ac_magento_gold`.`catalog_product_entity`
ON (`catalog_product_flat_1`.`entity_id` = `catalog_product_entity`.`entity_id`) AND (`catalog_product_entity`.`sku` = `catalog_product_flat_1`.`sku`) AND (`catalog_product_flat_1`.`sku` NOT REGEXP '(SZ|SIZE|GIFT)')
INNER JOIN `ac_magento_gold`.`catalog_product_entity_text`
ON (`catalog_product_entity_text`.`entity_id` = `catalog_product_entity`.`entity_id`) AND (`catalog_product_entity_text`.`attribute_id`= 61)
INNER JOIN `ac_magento_gold`.`cataloginventory_stock_item`
ON (`cataloginventory_stock_item`.`product_id` = `catalog_product_entity`.`entity_id`) AND (`cataloginventory_stock_item`.`product_id` = `catalog_product_flat_1`.`entity_id`) AND (`catalog_product_entity_text`.`entity_id` = `cataloginventory_stock_item`.`product_id`) AND (`cataloginventory_stock_item`.is_in_stock = 1) LIMIT 6;
If you have multiple websites or stores (or even stock_id), that could be the reason you are getting duplicates. You need to specify the relevant store_id or website_id or stock_id in your joins.
For example,
INNER JOIN `ac_magento_gold`.`catalog_product_entity_text`
ON (`catalog_product_entity_text`.`entity_id` = `catalog_product_entity`.`entity_id`)
AND (`catalog_product_entity_text`.`attribute_id`= 61)
should be
INNER JOIN `ac_magento_gold`.`catalog_product_entity_text`
ON (`catalog_product_entity_text`.`entity_id` = `catalog_product_entity`.`entity_id`)
AND (`catalog_product_entity_text`.`attribute_id`= 61)
AND `catalog_product_entity_text`.`store_id` = 0
or whatever your store_id is. Actually, the query makes even more sense (to me at least) with the parentheses rearranged like so:
INNER JOIN `ac_magento_gold`.`catalog_product_entity_text`
ON (
`catalog_product_entity_text`.`entity_id` = `catalog_product_entity`.`entity_id`
AND `catalog_product_entity_text`.`attribute_id`= 61
AND `catalog_product_entity_text``.store_id` = 0
)
You might also have to rewrite the following:
INNER JOIN `ac_magento_gold`.`cataloginventory_stock_item`
ON (`cataloginventory_stock_item`.`product_id` = `catalog_product_entity`.`entity_id`)
AND (`cataloginventory_stock_item`.`product_id` = `catalog_product_flat_1`.`entity_id`)
AND (`catalog_product_entity_text`.`entity_id` = `cataloginventory_stock_item`.`product_id`)
AND (`cataloginventory_stock_item`.is_in_stock = 1)
to:
INNER JOIN `ac_magento_gold`.`cataloginventory_stock_item`
ON (`cataloginventory_stock_item`.`product_id` = `catalog_product_entity`.`entity_id`
AND `cataloginventory_stock_item`.`product_id` = `catalog_product_flat_1`.`entity_id`
AND `catalog_product_entity_text`.`entity_id` = `cataloginventory_stock_item`.`product_id`
AND `cataloginventory_stock_item`.is_in_stock = 1
AND `cataloginventory_stock_item`.`stock_id` = 1)
again, depending on which stock_id you are interested in. As far as I know, catalog_product_entity only contains one of each entity_id, so no website_id, store_id, or stock_id has to be specified. I also believe that catalog_product_flat_1 is specific to one store_id or website_id or something, but I am not sure.
I'm really new here and I really need help. I am trying to update a table based on data joined from another select statement.
SELECT DISTINCT A.business_unit_AP
, A.VOUCHER_ID
, A.JOURNAl_ID
, A.UNPOST_SEQ
, A.APPL_JRNL_ID
, A.PYMNT_CNT
, A.VOUCHER_LINE_NUM
, A.DISTRIB_LINE_NUM,A.dst_acct_type
, A.LEDGER
, A.PROCESS_INSTANCE
FROM PS_PROJ_RES_TMP A
LEFT OUTER JOIN ps_proj_res_cal_vw B
ON a.business_unit_ap = B.BUSINESS_UNIT_AP
AND B.PROJECT_ID = A.PROJECT_ID
AND A.ACTIVITY_ID = B.ACTIVITY_ID
AND A.RESOURCE_ID = B.RESOURCE_ID
WHERE A.SYSTEM_SOURCE ='BAP'
AND b.business_unit_ap is null*
So basically the statement above select data from PS_PROJ_RES_TMP table that doesn't exist in PS_PROJ_RES_CAL_VW. Am i right? Then based on the data pulled, I will then update PS_VCHR_ACCTG_LINE.
I formulated this script but its taking too long. And i ended up updating the whole table.
UPDATE PS_VCHR_ACCTG_LINE C
SET C.PC_DISTRIB_STATUS = 'N'
WHERE EXISTS
(
SELECT DISTINCT A.business_unit_AP
, A.VOUCHER_ID
, A.JOURNAl_ID
, A.UNPOST_SEQ
, A.APPL_JRNL_ID
, A.PYMNT_CNT
, A.VOUCHER_LINE_NUM
, A.DISTRIB_LINE_NUM
, A.dst_acct_type
, A.LEDGER
, A.PROCESS_INSTANCE
FROM PS_PROJ_RES_TMP A
LEFT OUTER JOIN ps_proj_res_cal_vw B
ON a.business_unit_ap = B.BUSINESS_UNIT_AP
AND B.PROJECT_ID = A.PROJECT_ID
AND A.ACTIVITY_ID = B.ACTIVITY_ID
AND A.RESOURCE_ID = B.RESOURCE_ID
WHERE b.business_unit_ap is null
AND b.PROJECT_ID is null
AND b.ACTIVITY_ID is null
AND b.RESOURCE_ID is null
AND C.BUSINESS_UNIT = a.business_unit_ap
AND C.VOUCHER_ID = A.VOUCHER_ID
AND C.unpost_seq = A.unpost_seq
AND c.appl_jrnl_id = A.appl_jrnl_id
AND c.PYMNT_CNT = A.pymnt_cnt
AND C.voucher_line_num = A.voucher_line_num
AND C.distrib_line_num = A.distrib_line_num
AND C.dst_acct_type = A.dst_acct_type
AND C.ledger = A.ledger
AND A.SYSTEM_SOURCE ='BAP'
);
Where did i go wrong? Thank you so much for the help :)
It is pretty much impossible for an outsider to figure out the issues with your query. The problem of updating all the rows would seem to be because the logic in the query, on the data in the table, causes a match for all rows.
There is one easy thing you can do to improve performance. The distinct is not necessary when using exists. So the following should be equivalent:
UPDATE PS_VCHR_ACCTG_LINE C
SET C.PC_DISTRIB_STATUS = 'N'
WHERE EXISTS
(
SELECT 1
FROM PS_PROJ_RES_TMP A
LEFT OUTER JOIN ps_proj_res_cal_vw B
ON a.business_unit_ap = B.BUSINESS_UNIT_AP
AND B.PROJECT_ID = A.PROJECT_ID
AND A.ACTIVITY_ID = B.ACTIVITY_ID
AND A.RESOURCE_ID = B.RESOURCE_ID
WHERE b.business_unit_ap is null
AND b.PROJECT_ID is null
AND b.ACTIVITY_ID is null
AND b.RESOURCE_ID is null
AND C.BUSINESS_UNIT = a.business_unit_ap
AND C.VOUCHER_ID = A.VOUCHER_ID
AND C.unpost_seq = A.unpost_seq
AND c.appl_jrnl_id = A.appl_jrnl_id
AND c.PYMNT_CNT = A.pymnt_cnt
AND C.voucher_line_num = A.voucher_line_num
AND C.distrib_line_num = A.distrib_line_num
AND C.dst_acct_type = A.dst_acct_type
AND C.ledger = A.ledger
AND A.SYSTEM_SOURCE ='BAP';
If your original query runs quickly, then I would suggest putting the results in a temporary table and using that for the update.
I would speculate that the performance problem is actually due to ps_proj_res_cal_vw, which I further speculate is a view of some sort. MySQL doesn't always do a good job with optimizing views.
I have to write an SQL statement which contain a field that contain two different values consecutively but in the way I have wrote it, it return always null because it is interpreted as having the two value in the same time!
My conditions should be : (ci.field = 'Group' and ci.oldString = 'Triage' ) and (ci.field='assignee' and ci.newString is not NULL)
That means calculate time between: when the issue is assigned to group named Triage and when the issue is assigned to a person.
How can I fix it?
My SQL statement:
select TIMEDIFF(a.created,b.created)
from
(select g.created, g.issueid as groupid1
from changegroup g
join changeitem ci on (ci.groupid = g.id)
join jiraissue ji on (ji.id = g.issueid)
join project p on (p.id = ji.project)
join priority pr on (pr.id = ji.priority)
where ci.field = 'Group'
and ci.oldString = 'Triage'
and ci.field='assignee'
and ci.newString is not NULL
and p.pname = 'Test'
and pr.pname='P1'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) a
left join (
select ji.created, ji.id as groupid2
from jiraissue ji
join changegroup g on (g.issueid = ji.id)
join project p on (p.id = ji.project)
where p.pname = 'Test'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) b ON (a.groupid1 = b.groupid2);
This is the table from which I should retrieve data
See my comment about the quality of your question but a hint at how to solve this goes like (assuming you can make sure this doesn't create 1-n joins)
select groupid_orsomething_else, TIMEDIFF(a.created, b.created)
from yourtable
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'Group' and oldstring is 'Triage'
) a
on a.groupid_orsomething_else = yourtable.groupid_orsomething_else
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'assignee' and oldstring is null) b
on b.groupid_orsomething_else = yourtable.groupid_orsomething_else