Error when trying to run an update query using innerjoin (MySQL) - mysql

I try to use the following code in a MySQL query but get the error "Operation must use an updateable query" when I try to run it. However if I create another simple update query (without joins and subqueries) it works fine. Stumped at this point. Any ideas?
UPDATE IPad
SET Ipad.[case number] = (
SELECT ipad_damage_history.[case number]
FROM IPad
INNER JOIN ipad_damage_history ON IPad.[Apple ID] = ipad_damage_history.[Apple ID]
WHERE IPad.[case number] IS NULL
AND IPad.[Apple ID] = ipad_damage_history.[Apple ID]
)
WHERE Ipad.[Apple ID] = (
SELECT ipad_damage_history.[Apple ID]
FROM IPad
INNER JOIN ipad_damage_history ON IPad.[Apple ID] = ipad_damage_history.[Apple ID]
WHERE IPad.[case number] IS NULL
AND IPad.[Apple ID] = ipad_damage_history.[Apple ID]
);

The [] around identifiers is a SQL SERVER thing. In mysql you should use ` around multi word columns or reserved keywords.
And mysql update allows you to do the inner join directly:
UPDATE IPad
INNER JOIN ipad_damage_history
ON IPad.`Apple ID` = ipad_damage_history.`Apple ID`
SET Ipad.`case number` = ipad_damage_history.`case number`
WHERE IPad.`case number` IS NULL
AND IPad.`Apple ID` = ipad_damage_history.`Apple ID`

Related

How to write the query without using sub query?

I have this query and it works perfectly but I wanted to improve it and write it without sub-query. Is this possible? Although I have tried a lot, I am not able to get correct results.
SELECT * FROM store_product
WHERE product_id NOT IN (
SELECT entity_id
FROM import_pack_file_element
WHERE entity_id IS NOT NULL
AND import_pack_file_id IN (135)
AND status = 'DONE') AND store_id = 65
You can rewrite a NOT EXISTS or NOT IN query to an anti join. I don't see any reason, however, why you'd want to do that. Anti joins are usually used on DBMS that have problems with the straight-forward [NOT] IN / [NOT] EXISTS, which should not be the case with MySQL. Anti joins are a means to defend against a DBMS weakness and come at the price of loss of readability.
SELECT sp.*
FROM store_product sp
LEFT JOIN import_pack_file_element ipfe
ON ipfe.entity_id = sp.product_id
AND ipfe.import_pack_file_id = 135
AND ipfe.status = 'DONE'
WHERE ipfe.entity_id IS NULL;
You can use LEFT JOIN and IS NULL condition
SELECT store_product.*
FROM store_product
LEFT JOIN import_pack_file_element ON store_product.product_id = import_pack_file_element.entity_id
AND import_pack_file_element.import_pack_file_id IN (135)
AND import_pack_file_element.status = 'DONE'
WHERE
import_pack_file_element.entity_id IS NULL
You could use an EXISTS clause:
SELECT *
FROM store_product sp
WHERE NOT EXISTS (SELECT 1 FROM import_pack_file_element i
WHERE i.entity_id = sp.product_id AND
import_pack_file_id = 135 AND status = 'DONE');
You can try this(but better use Subquery instead):
SELECT [table alias].*
FROM store_product [table alias]
LEFT JOIN import_pack_file_element [table alias] on [table alias].[key] = [table alias].[key]
WHERE [table alias].entity_id IS NOT NULL AND [table alias].import_pack_file_id IN(135) AND [table alias].status = 'DONE'
You can modify LEFT JOIN part and WHERE part as your needs.
also this : [key] to your table key, [table alias] to your table alias.
and also this, u can replace IN(135) and use equals sign =, IN is use for more than one element that u need to compare or validate.
and here the reference :
LEFT JOIN
I don't have the full knowledge about your table design but this might work:
SELECT * FROM store_product x LEFT OUTER JOIN import_pack_file_element y
ON x.product_id = y.entity_id
WHERE x.product_id IS NULL
AND import_pack_file_id = 135
AND status = 'DONE'

How to combine UPDATE query with subquery?

In my Mysql database I want to update a field fup for all records which I found using this select statement:
SELECT ic.hash
FROM incompany as ic, app
WHERE ic.id = app.ic_id;
So I created the following combined query:
UPDATE incompany
SET fup = 'x'
WHERE hash IN (SELECT ic.hash
FROM incompany as ic, app
WHERE ic.id = app.ic_id);
But this query gives me the following error:
You can't specify target table 'incompany' for update in FROM clause
Does anybody know how I can make this work? All tips are welcome!
You seem to want a condition on two columns, so this is a bit tricky. If I follow the logic correctly:
UPDATE incompany ic JOIN
(SELECT DISTINCT ic.hash
FROM incompany ic JOIN
app
ON ic.id = app.ic_id
) ica
ON ica.hash = ic.hash
SET fup = 'x' ;
You do not need a subquery. You might use INNER JOIN to set the criteria to link the incompany table to the app table and to the another alias for incompany table.
UPDATE incompany I
INNER JOIN app A ON I.id = A.ic_id
INNER JOIN incompany I2 ON I.hash = I2.hash
SET I.fup = 'x'

MySql Inner Join Query

I'm using mysql connector to connect visual basic with mysql,i m doing mysql query with 3 tables and i tried with inner join and the "normal mode" with the "normal mode" said not unique table/alias and with inner join the datagrid doesnt load anything,the three table are this ones
Order(N_Order,Date,Client Number)
Line_Order(N_Order,product_code,quantity)
Product(product_code,name,price)
and the mysql query with innerjoin is:
"SELECT c.name, COUNT( b.product_code ) AS cnt FROM order a " & _
"INNER JOIN line_order b ON a.number_order = b.number_order " & _
"INNER JOIN product c ON b.product_code = c.product_code " & _
"GROUP BY c.name " & _
"ORDER BY cnt DESC "
and the normal way is:
"SELECT product.name, COUNT( order_line.product_code ) AS cnt
FROM order, product, order_line where order.number_order = order_line.number_order
AND order_line.product_code = product.product_code
GROUP BY product.name
ORDER BY cnt DESC
LIMIT 0 , 5"
When i run the 2ยบ mysql query in phpmyadmin it works perfectly but when i run it in visual basic it gives me the error not unique tables alias/order i dont know what to do can someone help me please??
Put backticks ` around the table `order` as it conflicts with the reserved keyword in ORDER BY.
Its Solved it was from the datagridview size was too little for the data,2 hours on this because of the datagridview size,thanks guys for the help

Access sql statement with multiple sums and joins

I am trying to get the sum of two different fields from different tables but no matter what i do one of the fields gets doubled. Below is my statement. Any help would be greatly appreciated it!
SELECT subq.[Opportunity ID],
sum(subq.[Buy Price]) as [Buy Price],
sum(subq.ProjectAmt) as [ProjectAmt]
FROM ( SELECT Distinct s.[Opportunity ID] as [Opportunity ID],
s.[Buy Price] as [Buy Price],
i.[ProjectAmt] as [ProjectAmt]
FROM SF_SKU_Data s
Inner join [ProjectInvoice] i on i.[Opportunity ID] = s.[Opportunity ID]
Group by s.[Opportunity ID], s.[Buy Price], i.[ProjectAmt]
) AS subq
GROUP BY subq.[Opportunity ID]

MySQL Update query with left join and group by

I am trying to create an update query and making little progress in getting the right syntax.
The following query is working:
SELECT t.Index1, t.Index2, COUNT( m.EventType )
FROM Table t
LEFT JOIN MEvents m ON
(m.Index1 = t.Index1 AND
m.Index2 = t.Index2 AND
(m.EventType = 'A' OR m.EventType = 'B')
)
WHERE (t.SpecialEventCount IS NULL)
GROUP BY t.Index1, t.Index2
It creates a list of triplets Index1,Index2,EventCounts.
It only does this for case where t.SpecialEventCount is NULL. The update query I am trying to write should set this SpecialEventCount to that count, i.e. COUNT(m.EventType) in the query above. This number could be 0 or any positive number (hence the left join). Index1 and Index2 together are unique in Table t and they are used to identify events in MEvent.
How do I have to modify the select query to become an update query? I.e. something like
UPDATE Table SET SpecialEventCount=COUNT(m.EventType).....
but I am confused what to put where and have failed with numerous different guesses.
I take it that (Index1, Index2) is a unique key on Table, otherwise I would expect the reference to t.SpecialEventCount to result in an error.
Edited query to use subquery as it didn't work using GROUP BY
UPDATE
Table AS t
LEFT JOIN (
SELECT
Index1,
Index2,
COUNT(EventType) AS NumEvents
FROM
MEvents
WHERE
EventType = 'A' OR EventType = 'B'
GROUP BY
Index1,
Index2
) AS m ON
m.Index1 = t.Index1 AND
m.Index2 = t.Index2
SET
t.SpecialEventCount = m.NumEvents
WHERE
t.SpecialEventCount IS NULL
Doing a left join with a subquery will generate a giant
temporary table in-memory that will have no indexes.
For updates, try avoiding joins and using correlated
subqueries instead:
UPDATE
Table AS t
SET
t.SpecialEventCount = (
SELECT COUNT(m.EventType)
FROM MEvents m
WHERE m.EventType in ('A','B')
AND m.Index1 = t.Index1
AND m.Index2 = t.Index2
)
WHERE
t.SpecialEventCount IS NULL
Do some profiling, but this can be significantly faster in some cases.
my example
update card_crowd as cardCrowd
LEFT JOIN
(
select cc.id , count(1) as num
from card_crowd cc LEFT JOIN
card_crowd_r ccr on cc.id = ccr.crowd_id
group by cc.id
) as tt
on cardCrowd.id = tt.id
set cardCrowd.join_num = tt.num;