I am trying to update the table T1 by counting the results of a SQL Query from other tables T2 and T3.
This is the query I came up with but it does not seem to work:
UPDATE T1
set Stock =
(SELECT count(ID_Item)
FROM T2,T3
WHERE T2.ID_Product = T1.ParentSKU AND
T3.ID = T2.ID_Product AND
Stock_Items.Name = '' AND
Stock_Items.Returned = ''
GROUP BY(T3.Size)
)
What I am trying to do is to update T1 by counting the results from T2 and grouping distinct sizes from T3.
You need to remove the GROUP BY, if your item has more than one size your subquery will return multiple results, and the update will fail.
UPDATE T1
set Stock =
(SELECT count(ID_Item)
FROM T2 INNER JOIN T3
WHERE T2.ID_Product = T1.ParentSKU AND
T3.ID = T2.ID_Product AND
Stock_Items.Name = '' AND
Stock_Items.Returned = '' AND
T3.Size = T1.Size
)
if Size is not a factor, then remove it completely from the subquery
Related
Update table2 t2
set t2.c1=t1.c1
inner join table1 t1 on ( t1.c2 = t2.c2)
where t2.c1 = "-1";
I want to execute the above query which will update the table2 column from table1 column ON INNER JOIN matching conditions. It is working fine. I am running a migration where the rows count are in million in both tables. I thought of limiting the update query in batches for query optimization but limit is not allowed in update query.
I can try with select query with limit option, but updating multiple columns would not work with this below query.
update table2 t2
set t2.c1=<?>
where t1.c2 = ( select c2 from table);
Can anyone help to use update query with optimization? Will updating millions row have any impact?
You could move the limiting clause to the joined table, like so:
update table2 t2
inner join (
select c1, c2
from table1
order by c2
limit ?, ?
) t1 on t1.c2 = t2.c2
set t2.c1 = t1.c1
where t2.c1 = -1
I am not sure what you really want to do, but you can update multiple columns with update and limit.
The following is fine:
update table2 t2
set t2.c1 = <?>,
t2.c3 = ?
where t1.c2 = ( select c2 from table)
limit 100;
I'm trying to do an update using an inner join with limit and order by (although the order by is not essential. From what I have read up the standard update will not work... this is what I am trying to do:
UPDATE table1
INNER JOIN table2
ON table1.service_id=table2.service_id
SET table1.flags = NULL
WHERE table1.type = 'fttc'
AND table1.flags = 'co'
AND table2.sync not like '%Yes%'
AND table1.date >= $today_date
ORDER BY table1.priority ASC
LIMIT 20;
it is for use in a case management tool and using php, I want to update 20 tickets i.e. remove the 'flag' so that they can be worked, the quantity will be passed as a variable, so I want to update 20 tickets for example highest 'priority' first, if that can be done?
If I read your question correctly, you want to perform an update on the first 20 records which results from the join, using the priority as ordering. You cannot do this directly in an UPDATE in MySQL AFAIK, but you can create an updatable view and then update that.
CREATE VIEW yourView
AS
SELECT
t1.service_id,
t2.service_id,
t1.flags,
t1.type,
t1.date,
t1.priority,
t2.sync
FROM table1 t1
INNER JOIN table2 t2
ON t1.service_id = t2.service_id
WHERE t1.type = 'fttc' AND
t1.flags = 'co' AND
t2.sync NOT LIKE '%Yes%' AND
t1.date >= $today_date
ORDER BY t1.priority
LIMIT 20;
And then update this view:
UPDATE yourView
SET flags = NULL
There should be no reason to use a view:
UPDATE table1 t1
SET t1.flags = NULL
WHERE t1.type = 'fttc' AND
t1.flags = 'co' AND
t1.date >= $today_date AND
EXISTS (SELECT 1
FROM table2 t2
WHERE t2.service_id = t1.service_id AND
t2.sync not like '%Yes%'
)
ORDER BY t1.priority ASC
LIMIT 20;
You cannot use ORDER BY and LIMIT with a multiple table JOIN. However, you can move the condition on table2 to the WHERE clause.
Following work for me:
UPDATE child AS upd
JOIN (SELECT t1.id FROM child AS t1
INNER JOIN master AS t2
ON t2.id = t1.id
where 1
AND t2.`date` BETWEEN '2020-06-23 00:00:00' AND '2020-06-23 23:59:59'
AND t2.client_id= 10 AND t1.code NOT IN('11','22')
order by t1.id desc LIMIT 1) AS col
ON upd.id=col.id
SET upd.code= '33', upd.`resp` = 'done',upd.status='success'
Here's my questions in writing some scripts in MySQL:
I get a table T1 with some columns called id, t1_col_01, t1_col_02, and a table T2 with some columns called id, t2_col_01, t2_col_02.
For each row R1 in T1, I want to update R1.t1_col_01 = 'Yes' if the there are multiple rows in T2 that has the same id column with R1.id. If not, set R1.t1_col_01 = 'No'.
I tried to write:
update T1, T2
set
T1.t1_col_01 = 'Yes'
where
(select count(*) from T2 where T2.id = T1.id) > 1
But it didn't work.
What you need is this:
update T1
inner join T2
on ( T2.id = T1.id )
set T1.t1_col_01 = 'Yes'
where (select count(*) from T2 where T2.id = T1.id) > 1
See it here on fiddle:
http://sqlfiddle.com/#!2/0edc4/1
I have been using the following SQL:
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event
WHERE t2.Event IS NULL
To select all rows from table 1 where table 2 is Null. This effectively filters out all my Table 1 data where Table 2 has data. However, I want to apply this only when a column in table 2 equals a certain value. Therefore I am looking to do a SELECT * FROM t2 WHERE t2.ID = 1 but am unsure how this fits into this query.
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event and t2.certain_column = 1234
WHERE t2.Event IS NULL
Also you can try query with NOT EXISTS:
SELECT DISTINCT NAME
FROM Events t1
WHERE NOT EXISTS(SELECT * FROM UserHistory t2
WHERE t1.Name = t2.Event AND t2.ID = 1)
You need to add the predicate to the JOIN condition:
SELECT DISTINCT NAME
FROM Events t1
LEFT JOIN UserHistory t2 ON t1.Name = t2.Event AND t2.ID = 1
WHERE t2.Event IS NULL;
If you add it to the WHERE you effectively turn your outer join into an inner join, meaning no rows will be returned (since NULL = 1 evaluates to false)
SELECT T2.ITEM_ID AS item_ITEM_ID
FROM types T
JOIN items T2
ON T.ITEM_ID = T2.ITEM_PARENT_ID
WHERE T.ITEM_TYPE = 'I'
AND T2.ITEM_TYPE = 'I'
This query is returning rows from T2 that have duplicate T2.ITEM_PARENT_IDs. How would I specify this query to return only rows that with unique T2.ITEM_PARENT_IDs ?
Count the repeats and filter out when they're more than 1.
SELECT T2.ITEM_ID AS item_ITEM_ID, COUNT(*) dups
FROM types T
JOIN items T2
ON T.ITEM_ID = T2.ITEM_PARENT_ID
WHERE T.ITEM_TYPE = 'I'
AND T2.ITEM_TYPE = 'I'
GROUP BY item_ITEM_ID
HAVING dups = 1
DEMO