Update data using join query in mysql - mysql

The below query is working fine but not updating records in the database. In my database there is three records available for update, but it's not updating.
UPDATE vgm_details VD SET VD.job_id = ( select S.job_id from stuffings S JOIN vgm V ON S.booking_id = V.booking_id WHERE S.container_no = VD.container_no) where VD.job_id = 0;
i have attached screenshot of vgm_details table.

Instead of subquery (and related issue for scoping) You could use an update with join
UPDATE vgm_details VD
INNER JOIN stuffings S ON S.container_no = VD.container_no
INNER JOIN vgm V ON S.booking_id = V.booking_id
SET VD.job_id = S.job_id
WHERE VD.job_id = 0;

Related

Join two SQL tables from diferent queries

I'm trying to join these two queries. Can anyone help?
Query 1 -
SELECT provas.id, disciplinas.disciplina, disciplinas.grupo,salas.sala
FROM provas, disciplinas, horarios, salas
WHERE provas.id = provas.id AND provas.id_disciplina = disciplinas.id AND provas.id_horario = horarios.id AND provas.id_sala = salas.id AND provas.id_horario JOIN horarios ON ;
Query 2 -
SELECT dias.dia, meses.nome, horas.hora, minutos.minuto
FROM horarios, meses, dias, horas, minutos
WHERE horarios.id = horarios.id AND horarios.id_dia = dias.id AND horarios.id_hora = horas.id AND horarios.id_mes = meses.id AND horarios.id_minuto = minutos.id;
Main table
Second Main table
I want to associate the "provas" table with the "horario" but the horario table has more foreign keys
provas = exams
horarios = scheudule
I want to join the shedule on the exams table, but the sheudule have more foreign keys for tables "days", "months", "hours" and "minutes"
All involved tables
You can try the following query. I didn't run this to make sure no syntax errors. But you can see the concept. You can join with a result set of a second query as shown below. I suggest you to consider the joins in your original query again. You may change the joins in my answer as it fits for your purpose.
SELECT ps.id, das.disciplina, das.grupo,s.sala
FROM provas ps
inner join disciplinas das on ps.id_disciplina = das.id
inner join horarios hs on ps.id_horario = hs.id
inner join salas s on ps.id_sala = s.id
inner join (
SELECT d.dia, ms.nome, h.hora, m.minuto, hs.id hsid
FROM horarios hs
inner join meses ms on hs.id_mes = ms.id
inner join dias d on hs.id_dia = d.id
inner join horas h on hs.id_hora = h.id
inner join minutos m on hs.id_minuto = m.id) zz on ps.id_horario = zz.hsid
My solution (I prefer this way):
SELECT provas.id, disciplinas.disciplina, disciplinas.grupo,salas.sala ,dias.dia, meses.nome, horas.hora, minutos.minuto
FROM provas, disciplinas, horarios, salas, meses, dias, horas, minutos
WHERE provas.id = provas.id AND provas.id_disciplina = disciplinas.id AND provas.id_horario = horarios.id AND provas.id_sala = salas.id AND provas.id_horario AND horarios.id_dia = dias.id AND horarios.id_hora = horas.id AND horarios.id_mes = meses.id AND horarios.id_minuto = minutos.id;

MySQL can’t specify target table for update in FROM multiple table joins

I have searched for days on how to get around this error while trying to update a field from a multiple join table, with a minimum date from the same mutiple join tableset.
This is my Update statement:
update vtiger_projectmilestone
Inner Join vtiger_projectmilestonecf ON vtiger_projectmilestone.projectmilestoneid = vtiger_projectmilestonecf.projectmilestoneid
Inner Join vtiger_crmentity ON vtiger_projectmilestone.projectmilestoneid = vtcrmm.crmid
inner join vtiger_project on vtiger_project.projectid = vtiger_projectmilestone.projectid
Inner Join vtiger_crmentity vtcrmp ON vtcrmp.crmid = vtiger_project.projectid
set vtiger_projectmilestone.projectmilestonedate =
(select min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
)
where vtiger_projectmilestone.projectid =
(select vtiger_project.projectid from vtiger_project
INNER JOIN vtiger_crmentity vtcrmp ON vtiger_project.projectid = vtcrmp.crmid
where vtcrmp.deleted = 0 order by vtiger_project.projectid desc limit 1)
and vtcrmp.deleted = 0
and vtcrmm.deleted = 0
and (vtiger_projectmilestone.projectmilestonedate is null or vtiger_projectmilestonecf.cf_763 is null) ;
This is a real life update query, not just a simple one table relationship.
I got round it by creating a temp table, inserting the value, updating the destination table and dropping the temp table.
I would really like to get this right, because it will come up more often.
All assistance is appreciated.
Cheers
Bernard Bailey
As stated in this answer you can't use the target update table in a subquery, as you can see in your query
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join
--using target update table in query
vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
However i think that a work around is using the data from the table that you're updating, so instead of using joins, you could write some where conditions for example:
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_crmentity AS vtcrmm ON tvpt.projecttasknumber = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
--using the projectmilestoneid in a where clause
AND tvpt.projecttasknumber=vtiger_projectmilestone.projectmilestoneid
The caveat could be that probably you will get some performance issues, also, as I don't know the full schema, I can't tell if using other tables in the subquery instead of vtiger_projectmilestone will give you the right result

UPDATE table whith specific condition

I have 2 tables - products and product_categories.
These tables JOIN ON products.product_id = product_categories.product_id .
I want to UPDATE field published in table products which have condition product_categories.product_categories = 100 .
Try this
UPDATE bjvui_virtuemart_products as prod
INNER JOIN bjvui_virtuemart_product_categories as cat
ON prod.virtuemart_product_id =cat.virtuemart_product_id
SET prod.published ={value}
WHERE cat.virtuemart_category_id=100
Use UPDATE with JOIN
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
WHERE a.someColumn = [some_value]
For your case
UPDATE bjvui_virtuemart_products AS p
INNER JOIN bjvui_virtuemart_product_categories AS c ON
p.virtuemart_product_id = c.virtuemart_product_id
SET p.published = "your_value"
WHERE c.virtuemart_category_id = 100;

Inner Join Nested Inside Update Statement SQL

I am trying to write a query that performs an inner join within an update statement.
Here is the current query I am working with:
UPDATE
singulation1.`q096_cq33r08-ds01-n-testtable`
SET
visual = t2.visual,
inspection_status = t2.inspection_status,
inspector_name = t2.inspector_name
FROM
singulation1.`q096_cq33r08-ds01-n-testtable` t1
INNER JOIN
singulation1.`q014_bq31t05-dw30-x` t2
ON
(t1.print = t2.print AND t1.id3 = t2.id3);
Something about MySql does not like the FROM clause.
For updates, you specify the join in the update clause
UPDATE
singulation1.`q096_cq33r08-ds01-n-testtable` AS t1
INNER JOIN singulation1.`q014_bq31t05-dw30-x` AS t2
ON t1.print = t2.print AND t1.id3 = t2.id3
SET
t1.visual = t2.visual
t1.inspection_status = t2.inspection_status,
t1.inspector_name = t2.inspector_name

Unknown Column id in MySQL Query

Can someone help me I've got 2 MySQL queries that get unknown column id when I try to run them. I might add that I am converting this database from SQLServer 2005 to MySQL and they run fine in SQL Server 2005.
Here's 1 of them:
SELECT DISTINCT g.id AS `genre`
FROM media_playlist_sequence MPS
INNER JOIN media M ON M.`key` = MPS.media_key
INNER JOIN media_playlists MP ON MP.`key` = MPS.playlist_key
INNER JOIN node_media_playlist NMP ON NMP.playlist_key = MP.`key`
INNER JOIN nodes N ON N.`key` = NMP.node_key
INNER JOIN media_files MF ON MF.media_key = M.`key`
INNER JOIN media_locations ML ON ML.media_file_key = MF.media_file_key
AND ML.node_key = n.`key`
INNER JOIN media_genres MG ON MG.media_key = M.`key`
INNER JOIN genres G ON G.`key` = MG.genre_key
WHERE M.is_ready = 1
AND MP.id = 'Channels'
AND N.id = 'VIC-WIN7'
AND mf.is_quad_image = 0
My guess is that it's a case sensitivity issue. MySQL can be case sensitive by default whereas SQL Server is not.