update query with multiple inner joins - c#

I want to update the table with multiple inner join query but when I write the query it through an error incorrect syntax near inner keyword
Update inventory_detail INNER JOIN inventory
ON inventory_detail.inventory_id = Inventory.Inventory_id
INNER JOIN Ingredients
ON Inventory.Inventory_id=Ingredients.invenotry_id
SET inventory_detail.Quantity=inventory_detail.Quantity-1
WHERE inventory_detail.loc_id =1 AND Ingredients.item_id=27 ;
ERD diagram

Change your query to:
UPDATE inventory_detail
SET Quantity = Quantity - 1
FROM inventory_detail
INNER JOIN inventory
ON inventory_detail.inventory_id = Inventory.Inventory_id
INNER JOIN Ingredients
ON Inventory.Inventory_id = Ingredients.invenotry_id
WHERE inventory_detail.loc_id = 1
AND Ingredients.item_id = 27;

Related

Missing operator in 3 table join statement [duplicate]

Syntax Error (missing Operator) in query expression 'tbl_employee.emp_id = tbl_netpay.emp_id INNER JOIN tbl_gross ON tbl_employee.emp_id = tbl_gross.emp_ID INNER JOIN tbl_tax ON tbl_employee.emp_id - tbl_tax.emp_ID'.
SELECT tbl_employee.emp_ID,
tbl_employee.emp_name,
tbl_gross.BasicSalary,
tbl_gross.totalOT,
tbl_netpay.totalGross,
tbl_tax.totalLate,
tbl_tax.allowance,
tbl_tax.SSS,
tbl_tax.PhilHealth,
tbl_tax.GSIS,
tbl_tax.HDMF,
tbl_netpay.totalDeduc,
tbl_netpay.emp_ti,
tbl_netpay.emp_wt,
tbl_netpay.emp_np
FROM tbl_employee
INNER JOIN tbl_netpay ON tbl_employee.emp_id = tbl_netpay.emp_id
INNER JOIN tbl_gross ON tbl_employee.emp_id = tbl_gross.emp_ID
INNER JOIN tbl_tax ON tbl_employee.emp_id = tbl_tax.emp_ID;
I always get the error above.
Access requires parentheses in the FROM clause for queries which include more than one join. Try it this way ...
FROM
((tbl_employee
INNER JOIN tbl_netpay
ON tbl_employee.emp_id = tbl_netpay.emp_id)
INNER JOIN tbl_gross
ON tbl_employee.emp_id = tbl_gross.emp_ID)
INNER JOIN tbl_tax
ON tbl_employee.emp_id = tbl_tax.emp_ID;
If possible, use the Access query designer to set up your joins. The designer will add parentheses as required to keep the db engine happy.
Thanks HansUp for your answer, it is very helpful and it works!
I found three patterns working in Access, yours is the best, because it works in all cases.
INNER JOIN, your variant. I will call it "closed set pattern".
It is possible to join more than two tables to the same table with good performance only with this pattern.
SELECT C_Name, cr.P_FirstName+" "+cr.P_SurName AS ClassRepresentativ, cr2.P_FirstName+" "+cr2.P_SurName AS ClassRepresentativ2nd
FROM
((class
INNER JOIN person AS cr
ON class.C_P_ClassRep=cr.P_Nr
)
INNER JOIN person AS cr2
ON class.C_P_ClassRep2nd=cr2.P_Nr
)
;
INNER JOIN "chained-set pattern"
SELECT C_Name, cr.P_FirstName+" "+cr.P_SurName AS ClassRepresentativ, cr2.P_FirstName+" "+cr2.P_SurName AS ClassRepresentativ2nd
FROM person AS cr
INNER JOIN ( class
INNER JOIN ( person AS cr2
) ON class.C_P_ClassRep2nd=cr2.P_Nr
) ON class.C_P_ClassRep=cr.P_Nr
;
CROSS JOIN with WHERE
SELECT C_Name, cr.P_FirstName+" "+cr.P_SurName AS ClassRepresentativ, cr2.P_FirstName+" "+cr2.P_SurName AS ClassRepresentativ2nd
FROM class, person AS cr, person AS cr2
WHERE class.C_P_ClassRep=cr.P_Nr AND class.C_P_ClassRep2nd=cr2.P_Nr
;

How to join tables and queries in C# with access database

this is how i join 2 tables and select form it:
OleDbDataAdapter DataA = new OleDbDataAdapter(#"Select tfr.FeedID, tf.FeedName, tfr.FeedQuantity, tf.DM
FROM tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON (tf.FeedID=tfr.FeedID)", Connection);
but what about adding a access query to this select command?
for example I want to add this statement to my select command:
Select qfq.FeedDMQuantites
From qFeeds_Quantities as qfq
what should I do?
Well add another JOIN condition to this table qFeeds_Quantities (assuming you have a relationship to this table or a common column among other table).
Assuming you have a common column like FeedID in this new table as well you can make another JOIN like
select tfr.FeedID, tf.FeedName, tfr.FeedQuantity,
tf.DM, qfq.FeedDMQuantites
FROM (tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON tf.FeedID = tfr.FeedID)
INNER JOIN qFeeds_Quantities as qfq ON tf.FeedID = qfq.FeedID;
If you want to include another JOIN then parenthesize like
FROM ((tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON tf.FeedID = tfr.FeedID)
INNER JOIN qFeeds_Quantities as qfq ON tf.FeedID = qfq.FeedID)
INNER JOIN BLAH AS bll ON bll.test = tf.test;

How to remove repeated rows in this Access query?

How can I get rid of duplicated rows in my MS access query?
SELECT pt.code,
resis.resis,
sense.sense
FROM (sense INNER JOIN pt ON sense.code = pt.code)
INNER JOIN resis ON pt.code = resis.code;

Get value to display from another sql table

I have 2 tables as attached image:
In order to save space, i only store the ID.
In the gridview, i want to show the name based on the ID.
I tried on inner join but only 1 parameter can be inner join.
Any direction can i go? Please help.
Here is the code i tried:
SelectCommand="SELECT * FROM [ProjectInfo] inner join [Employee] ON Employee.ID = ProjectInfo.ProjectInfo_Leader AND Employee.ID = ProjectInfo.ProjectInfo_CoLeader AND Employee.ID = ProjectInfo.ProjectInfo_Helper"
SelectCommand="SELECT * FROM [ProjectInfo] inner join [Employee] ON Employee.ID = ProjectInfo.ProjectInfo_Leader OR Employee.ID = ProjectInfo.ProjectInfo_CoLeader OR Employee.ID = ProjectInfo.ProjectInfo_Helper"
I am not familiar with SQL, thus i can only try and error...
Thanks.
It is obvious solution.
select l.name leader,c.name [co-lider],h.name helper
from role r
left join people l on r.lider=l.id
left join people c on r.[co-lider]=c.id
left join people h on r.helper=h.id
Note: it is better to use left join in case some roles are not filled.

how to get sum of this column in sql server

i'm use below query for fetch my data with an ordernumber...
SELECT Details.Quantity,
Details.OrderNumber,
Orders.OrderDate,
Products.ProductName,
Products.UnitPrice ,
Details.Quantity*Products.UnitPrice as qprice
FROM Details
INNER JOIN Orders
ON Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products
ON Details.ProductID = Products.ProductID
where Orders.OrderNumber='14195'
this query give me below result
now i want give sum of qprice column..
how can i do this??
Like this?
SELECT SUM(Details.Quantity * Products.UnitPrice) AS Amount
FROM Details
INNER JOIN Orders ON
Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products ON
Details.ProductID = Products.ProductID
WHERE Orders.OrderNumber = '14195'
If you need to have TotalAmount as an extra column to what you already have then:
SELECT
Details.Quantity,
Details.OrderNumber,
Orders.OrderDate,
Products.ProductName,
Products.UnitPrice,
Details.Quantity*Products.UnitPrice as qprice,
SUM(Details.Quantity*Products.UnitPrice) OVER() TotalAmount
FROM Details
INNER JOIN Orders ON
Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products ON
Details.ProductID = Products.ProductID
WHERE Orders.OrderNumber = '14195'

Categories