Inner Join Nested Inside Update Statement SQL - mysql

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

Related

Update data using join query in 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;

Joining mutiple select statements in one SQL statement

I am trying to join two sql statements into one but have had no success.
I would ideally like the Average query to be added to the end of the columns from the first sql statement.
First statement:
SELECT Modules.UserID, Module_Info.ModuleTitle, Modules.ModuleMarks, Modules.ExamMark, Modules.AssignmentMark, MarkClassification.MarkDescription
FROM Module_Info
INNER JOIN Modules ON Module_Info.ModuleID = Modules.ModuleID
INNER JOIN MarkClassification ON Modules.MarkCodeDescription = MarkClassification.MarkId
WHERE(Modules.UserID = '8')
Second statement:
SELECT AVG(Modules.ExamMark) AS Average
FROM Module_Info
INNER JOIN Modules ON Module_Info.ModuleID = Modules.ModuleID
WHERE (Modules.ModuleID = '2')
You can use a sub query in the select for that, like this:
SELECT Modules.UserID, Module_Info.ModuleTitle, Modules.ModuleMarks,
Modules.ExamMark, Modules.AssignmentMark, MarkClassification.MarkDescription,
(SELECT AVG(Modules.ExamMark)
FROM Module_Info
INNER JOIN Modules ON Module_Info.ModuleID = Modules.ModuleID
WHERE (Modules.ModuleID = '2')) as Average
FROM Module_Info
INNER JOIN Modules
ON Module_Info.ModuleID = Modules.ModuleID
INNER JOIN MarkClassification
ON Modules.MarkCodeDescription = MarkClassification.MarkId
WHERE(Modules.UserID = '8')
you can try
SELECT * FROM
(SELECT Modules.UserID, Module_Info.ModuleTitle, Modules.ModuleMarks, Modules.ExamMark, Modules.AssignmentMark, MarkClassification.MarkDescription
FROM Module_Info
INNER JOIN Modules ON Module_Info.ModuleID = Modules.ModuleID
INNER JOIN MarkClassification ON Modules.MarkCodeDescription = MarkClassification.MarkId
WHERE(Modules.UserID = '8')) t1
JOIN
(SELECT AVG(Modules.ExamMark) AS Average
FROM Module_Info
INNER JOIN Modules ON Module_Info.ModuleID = Modules.ModuleID
WHERE (Modules.ModuleID = '2')) t2
Since you do not have a join condition, it will create a cartesian product of the two queries (i.e. each row of the first subselect, with each row of the second subselect). Since the 2nd subselect only has one row, it will give you your expected result.

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

How to wirte a left join query in codeigniter?

can some one tell me how to write this mysql query in codeigniter??
SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
Try this
$this->db->select(*);
$this->db->from('t1');
$this->db->join('t2','t2.a = t1.a','left');
$this->db->join('t3','t3.b = t1.b','left');
$this->db->join('t4','t4.c = t1.c','left');
Alternatively (condensed)
$this->db->select(*)->from('t1')->join('t2','t2.a = t1.a','left')->join('t3','t3.b = t1.b','left')->join('t4','t4.c = t1.c','left');
Reference: http://ellislab.com/codeigniter/user-guide/database/active_record.html

Linq to SQL with more conditiones on one join

I'm new to Linq to SQL and I'm trying to transform this SQL into Linq. Could you please help me out.
SELECT *
FROM [dbo].[tblTest]
INNER JOIN [dbo].[tblStationTest] ON [tblTest].[id] = [tblStationTest].[Test_id]
INNER JOIN [dbo].[tblTestType] ON [tblTest].[TestType_id] = [tblTestType].[id]
LEFT OUTER JOIN [dbo].[tblTestOrder] ON [tblTest].[id] = [tblTestOrder].[Test_id]
AND ([tblTestOrder].[TestOrderList_id] = 1)
WHERE ([Station_id] = 1)
What is causing me problem is this condition AND ([TestOrderList_id] = 1)
This condition can't be in Where clase because it will cancel the effect of Left Join
Thanks kurin
you can try something like
from test in db.tblTest
join stationTest in db.tblStationTest on test.id equals stationTest.Test_id
join testType in db.tblTestType on test.TestType_id equals testType.id
join testOrder in db.tblTestOrder on new{Key1 = test.id, Key2= 1} equals new{Key1 = testOrder.Test_id, Key2 = testOrder.TestOrderList_id} into tempOrders
from t in tempOrders.DefaultIfEmpty()
select new{test.Test_id, testType.something, t.somethingelse}