UPDATE Recipes RE, (
SELECT SUM((((i.iCaseCost/i.iCaseQty)/i.iUnitSize)/i.iUnitSoldBy)*ri.riQty*ri.riMeasureBy) AS 'RecipeCost'
FROM Recipes r INNER JOIN RecipeIngredients ri
ON r.rID = ri.rID JOIN Ingredients i
ON ri.iID = i.iID
WHERE ri.rID = RE.rID
) t
SET RE.rYieldCost = t.RecipeCost
When executed, I get the following error: "Unknown column 'RE.rID' in 'where clause'".
Any ideas?
Your inner derived query doesn't know anything about columns in the outer query. Try moving the WHERE into the UPDATE clause with something like WHERE t.rID = RE.rID.
Related
my command:
Select sobe.Id, sobe.Naziv, sobe.Opis, sobe.Kat, (Select Group_CONCAT(studenti.Ime,studenti.Prezime) ImePrezime FROM studentsoba LEFT JOIN studenti ON studentsoba.Id_Sobe=sobe.Id AND studenti.JMBAG = studentsoba.JMBAG) AS ImePrezime from Sobe
This gives me this error:
1054 - Unknown column 'sobe.Id' in 'on clause'
I see what the problem is but I cant figure out how to fix it.
I want to pass sobe.Id inside of this subquery:
(Select Group_CONCAT(studenti.Ime,studenti.Prezime) ImePrezime FROM studentsoba LEFT JOIN studenti ON studentsoba.Id_Sobe=sobe.Id AND studenti.JMBAG = studentsoba.JMBAG)
I want to check which students are in that room.
Word soba means room and JMBAG is like personal number for each student
Try moving it to a WHERE clause:
Select s.Id, sobe.Naziv, s.Opis, s.Kat,
(Select Group_CONCAT(si.Ime, si.Prezime) as ImePrezime
from studentsoba ss LEFT JOIN
studenti si
on si.JMBAG = ss.JMBAG
where ss.Id_Sobe = s.Id
) AS ImePrezime
from Sobe s;
The correlated subquery should be fine. I suppose the correlation clause cannot be in the on clause -- I normally put it in the where clause anyway.
Also note that I introduced table aliases so the query is easier to write and to read.
I don't know why this my DELETE sql command got the error number #1064.
My delete sql:
delete from nit.grades g where
(select id_dep from nit.offers as oo,nit.subjects as s where s.id=oo.id_subject and g.id_offer=oo.id)
!=(select id_dep from nit.students as stu where g.id_student=stu.id);
but this sql Select same where clause is working.
select * from nit.grades g where
(select id_dep from nit.offers as oo,nit.subjects as s where s.id=oo.id_subject and g.id_offer=oo.id)
!=(select id_dep from nit.students as stu where g.id_student=stu.id);
thanks for any help.
the Error message:
The syntax with the alias of the table you use for the delete statement is wrong.
Even more in the subqueries you use the target table and this is not allowed in MySql.
Instead you should use joins.
From your code this is what I understood that you want:
delete g
from nit.grades g
inner join nit.offers oo on g.id_offer = oo.id
inner join nit.subjects s on s.id = oo.id_subject
inner join nit.students st on g.id_student = st.id
where st.id_dep <> s.id_dep
In the WHERE clause I'm not sure if the columns id_dep are qualified correctly because they are not qualified also in your code.
If this is not what you want to do then use your SELECT query which does work (as you say) as a join to the table, provided there is a primary key like id in nit.grades:
delete g
from nit.grades g
inner join (
<your select query here>
) t
on t.id = g.id
I'm trying to update every row of my invoices table to hold the tax amount of that invoice. This is calculated by getting the tax percentage associated with the vendor and multiplying it by the invoice amount (obviously). My problem is that I'm trying to update the table with data from the same table joined with others. Currently, my query shown below gives errors of:
Error Code: 1093. Table 'tblVendorInvoices' is specified twice, both as a target for 'UPDATE' and as a separate source for data
when I remove the WHERE statement &
Error Code: 1054. Unknown column 'a.VENDORINVOICEID' in 'where clause'
With the WHERE.
Here is my query:
UPDATE tblVendorInvoices SET VdrTaxAmount =
(SELECT round(VdrInvoiceAmount*TaxAmount,2) FROM tblVendorInvoices a
LEFT JOIN tblVendors ON a.VendorName = tblVendors.VendorID
LEFT JOIN tblTax on tblVendors.vdrtaxid = tblTax.TAXID)
WHERE a.VENDORINVOICEID = tblVendorInvoices.VENDORINVOICEID;
This should work
UPDATE tblVendorInvoices SET VdrTaxAmount = c.Amount
from
(SELECT VENDORINVOICEID,round(VdrInvoiceAmount*TaxAmount,2) Amount FROM tblVendorInvoices a
LEFT JOIN tblVendors ON a.VendorName = tblVendors.VendorID
LEFT JOIN tblTax on tblVendors.vdrtaxid = tblTax.TAXID)c
WHERE c.VENDORINVOICEID = tblVendorInvoices.VENDORINVOICEID;
In MySQL, use a correlated subquery. I think this is the logic you want:
UPDATE tblVendorInvoices i
SET VdrTaxAmount =
(SELECT round(VdrInvoiceAmount*TaxAmount,2)
FROM tblVendors v JOIN
tblTax t
ON v.vdrtaxid = t.TAXID
WHERE i.VendorID = v.VendorID
);
Try this
UPDATE tblVendorInvoices SET VdrTaxAmount =
(SELECT round(VdrInvoiceAmount*TaxAmount,2)
FROM tblVendorInvoices as A
LEFT JOIN tblVendors as V ON a.VendorName = V.VendorID
LEFT JOIN tblTax on V.vdrtaxid = tblTax.TAXID)
WHERE A.VENDORINVOICEID = V.VENDORINVOICEID;
Your getting that error because you have not distinguished what table and column to use in the where statement:
WHERE a.VENDORINVOICEID = tblVendorInvoices.VENDORINVOICEID;
You are using the same table and column.
You might also look at your first join statement because you might be trying to join a string and a INT and this will not work. You need to join on two columns that have the same data type. I don't know what type of data each of your columns has.
I'm using MySQL 5.5.37. I want to update a table and I would like to use the value of the specific row I wish to update the in the query clause I use to get an update value. So for instance, I alias my table like so
update session ts set
and so I would like to reference "ts.id" in the "set" portion of the statement, if that's possible. I tried
update session ts set user_id = (select q.* from
(select u.id FROM session t, training_session_org tso, organization o, user u
where o.user_id = u.id and tso.organization_id = o.id
and t.id = tso.training_session_id and t.id = ts.id) q);
But I'm getting a
ERROR 1054 (42S22): Unknown column 'ts.id' in 'where clause'
What's the proper way to reference my updated table in the query, if its possible? Also, I want to do this in one SQL statement, not multiple ones.
The problem is that you are using a double subquery and ts.id goes out of the inner query scope, I suspect that you tried to use it for avoiding the error:
You can't specify target table 'ts' for update in FROM clause
but the solution is just omitting the session table from the subquery as you don't need it:
update session set user_id = (select u.id
from training_session_org tso
join organization o on o.id = tso.organization_id
join user u on u.id = o.user_id
where tso.training_session_id=session.id)
In fact, based on the joins you can even simplify the query further:
update session set user_id = (select o.user_id
from training_session_org tso
join organization o on o.id = tso.organization_id
where tso.training_session_id=session.id)
I've got an annoying issue with an update query I'm trying to get working... The following statement SHOULD update channels.media_view_count to the result of the subquery (for all channels).
UPDATE channels c
SET c.media_view_count = (
SELECT SUM(t.view_count)
FROM (
SELECT DISTINCT m.viewkey, m.view_count
FROM media m
INNER JOIN participants p ON m.id = p.medium_id
WHERE p.user_id = c.id AND m.is_viewable = 1
AND (p.pending = 0)
) AS t
);
The subquery works fine independently (when specifying an actual id for c.id, like 47778 or whatever), but when I execute this statement, I get:
ERROR 1054 (42S22): Unknown column 'c.id' in 'where clause'
I thought I would be able to access the channels table (aliased as c) from within the subquery? Am I missing something or am I totally wrong here?
Any and all help is appreciated :)
Thanks,
Jeff
UPDATE channels c, (
SELECT t.user_id, SUM(t.view_count) cnt
FROM (
SELECT DISTINCT p.user_id, m.viewkey, m.view_count
FROM media m
INNER JOIN participants p ON m.id = p.medium_id
WHERE m.is_viewable = 1
AND (p.pending = 0)
) AS t GROUP BY t.user_id ) temp
SET c.media_view_count = temp.cnt
WHERE c.id = temp.user_id
Try like this... Did not test it though :) ..
Conceptually, it should work