I'm trying to update the fields by using below query. What is the error in below query ?
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'FROM orders cl' at line 2
update hedging SET
Dept = concat(RIGHT( dpt.DeptName,2),LEFT( grp.GroupName,3),LEFT( st.Login,3)),
OrderNo = cl.OrderNo,Client = cn.ShortName,
Currency = cur.Notation, SellingAmount = pto.PIAmount ,
BuyingAmount = pto.POAmount
FROM orders cl
left join client cn on cl.ClientID = cn.ClientID
inner join department dpt on cl.DeptID = dpt.DeptID
inner join supplier sp on cl.SupplierID = sp.SupplierId
left join staff st on st.StaffID = cl.SalesPerson
left join pipo_total pto on pto.OrderNo = cl.OrderNo
inner join groups grp on cl.GroupID = grp.GroupID
left join currency cur on cur.CurrencyID= cl.SellCurrencyID
left join hedging hed on hed.OrderNo = cl.OrderNo)
where cur.Notation <> 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo
Try 1
update orders cl
left join client cn on cl.ClientID = cn.ClientID
inner join department dpt on cl.DeptID = dpt.DeptID
inner join supplier sp on cl.SupplierID = sp.SupplierId
left join staff st on st.StaffID = cl.SalesPerson
left join pipo_total pto on pto.OrderNo = cl.OrderNo
inner join groups grp on cl.GroupID = grp.GroupID
left join currency cur on cur.CurrencyID= cl.SellCurrencyID
left join hedging hed on hed.OrderNo = cl.OrderNo
SET hed.Dept = concat(RIGHT( dpt.DeptName,2),LEFT( grp.GroupName,3),LEFT( st.Login,3))
,hed.OrderNo = cl.OrderNo
,hed.Client = cn.ShortName
,hed.Currency = cur.Notation
,hed.SellingAmount = pto.PIAmount
,hed.BuyingAmount = pto.POAmount
where cur.Notation <> 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo
UPDATE with multiple tables should be something like this
UPDATE table1 t1
INNER JOIN table2 t2 ON t1.ID = t2.ID
SET t1.value = [value]
Edit: Your updated query
update orders cl
left join client cn on cl.ClientID = cn.ClientID
inner join department dpt on cl.DeptID = dpt.DeptID
inner join supplier sp on cl.SupplierID = sp.SupplierId
left join staff st on st.StaffID = cl.SalesPerson
left join pipo_total pto on pto.OrderNo = cl.OrderNo
inner join groups grp on cl.GroupID = grp.GroupID
left join currency cur on cur.CurrencyID= cl.SellCurrencyID
left join hedging hed on hed.OrderNo = cl.OrderNo)
SET Dept = concat(RIGHT( dpt.DeptName,2),LEFT( grp.GroupName,3),LEFT( st.Login,3))
,OrderNo = cl.OrderNo
,Client = cn.ShortName
,Currency = cur.Notation
,SellingAmount = pto.PIAmount
,BuyingAmount = pto.POAmount
where cur.Notation <> 'USD' and cl.OrderType = '1' and hed.OrderNo = cl.OrderNo
Related
First, I searched about the same problem but I didn't find an appropriate solution.
My problem is with the following code, it's returning wrong results:
SELECT FbID,FhID,
FbRef,
FbDate,
(D.AccName) AS DName,
FbQuan,
CONCAT(CategoryName,'-',ProductName) AS ProdName,
(C.AccName) AS CusName,
FhPurPrice,FbSalePrice,
(R.AccName) AS ResoName,
Curr1.CurrencyName,
Curr2.CurrencyName,
Plc1.PlaceName AS FhResoPlaceName,
Plc2.PlaceName AS FbCusPlaceName,
'linked' AS xLinkStatus,
1 AS xStatus
FROM tblfatora2 F2
INNER JOIN tblfatora1 F1 ON F1.FhRef = F2.FhRef
INNER JOIN tblproducts P ON P.ProductID = F1.FhProduct
INNER JOIN tblcategories CT ON CT.CategoryID = P.ProductCategory
INNER JOIN tblaccounts R ON R.AccID = F1.FhReso
INNER JOIN tblaccounts C ON C.AccID = F2.FbCus
INNER JOIN tblaccounts D ON D.AccID = F1.FhDriver
INNER JOIN tblcurrencies Curr1 ON C.AccCurrID = Curr1.CurrencyID
INNER JOIN tblcurrencies Curr2 ON R.AccCurrID = Curr2.CurrencyID
LEFT JOIN tblplaces Plc1 ON F1.FhResoPlace = Plc1.PlaceID
LEFT JOIN tblplaces Plc2 ON F2.FbCusPlace = Plc2.PlaceID
WHERE FIND_IN_SET(`FhID`, '18313,18314')
ORDER BY FbDate, FbID
the results that it gives me are: enter image description here
note: I use FIND_IN_SET here because I can't use (IN) where I use that SQL statement in a procedure inside vb.net code:
Public Sub MySql_GetLinked()
xDtAll = New DataTable()
Dim xPar(0) As MySqlParameter
xPar(0) = New MySqlParameter("#FhID", MySqlDbType.String) With {
.Value = LinkedFatora}
xClsMySql.GetData(xSqlLinked, xDtAll, xPar)
End Sub
So I fill the variable (LinkedFatora) by loop and I use the same SQL statement but I replace (WHERE FIND_IN_SET('FhID', '18313,18314')) with (WHERE FIND_IN_SET('FhID', #FhID)).
I looked for the error's reason but couldn't catch it.
I found the problem, it's changing (WHERE FIND_IN_SET(FhID, '18313,18314')) to (WHERE FIND_IN_SET(FbID, '18313,18314')):
SELECT FbID,FhID,
FbRef,
FbDate,
(D.AccName) AS DName,
FbQuan,
CONCAT(CategoryName,'-',ProductName) AS ProdName,
(C.AccName) AS CusName,
FhPurPrice,FbSalePrice,
(R.AccName) AS ResoName,
Curr1.CurrencyName,
Curr2.CurrencyName,
Plc1.PlaceName AS FhResoPlaceName,
Plc2.PlaceName AS FbCusPlaceName,
'linked' AS xLinkStatus,
1 AS xStatus
FROM tblfatora2 F2
INNER JOIN tblfatora1 F1 ON F1.FhRef = F2.FhRef
INNER JOIN tblproducts P ON P.ProductID = F1.FhProduct
INNER JOIN tblcategories CT ON CT.CategoryID = P.ProductCategory
INNER JOIN tblaccounts R ON R.AccID = F1.FhReso
INNER JOIN tblaccounts C ON C.AccID = F2.FbCus
INNER JOIN tblaccounts D ON D.AccID = F1.FhDriver
INNER JOIN tblcurrencies Curr1 ON C.AccCurrID = Curr1.CurrencyID
INNER JOIN tblcurrencies Curr2 ON R.AccCurrID = Curr2.CurrencyID
LEFT JOIN tblplaces Plc1 ON F1.FhResoPlace = Plc1.PlaceID
LEFT JOIN tblplaces Plc2 ON F2.FbCusPlace = Plc2.PlaceID
WHERE FIND_IN_SET(`FbID`, '18313,18314')
ORDER BY FbDate, FbID
How can I optimize this join query? how can I get the latest start_dt in this query? Is any way can make it faster to load and the start_dt is the latest one? If I delete the subquery for this case, I will get the record which the start_dt is not the latest one. Please Help!!
SELECT
matrix.matrix_uuid,
matrix.name AS matrix_name,
courses.courses_uuid,
courses.name AS courses_name,
employees.employees_uuid,
CONCAT(employees.first_name,
' ',
employees.last_name) AS Name,
courses.validity,
courses.duration,
(SELECT
MAX(start_dt)
FROM
courses_schedule C
WHERE
C.courses_schedule_uuid = CS.courses_schedule_uuid) AS start_dt,
COUNT(courses.courses_uuid) AS refresher,
courses.courses_refresher_uuid,
courses_taken.overall_status,
courses_taken.status
FROM
employees
INNER JOIN
courses_taken ON courses_taken.employees_uuid = employees.employees_uuid
LEFT JOIN
courses_schedule CS ON CS.courses_schedule_uuid = courses_taken.courses_schedule_uuid
LEFT JOIN
matrix_courses matrix_courses ON matrix_courses.courses_uuid = CS.courses_uuid
LEFT JOIN
matrix ON matrix.matrix_uuid = matrix_courses.matrix_uuid
LEFT JOIN
courses ON courses.courses_uuid = matrix_courses.courses_uuid
WHERE
CS.start_dt = (SELECT
MAX(start_dt)
FROM
courses_taken CT
LEFT JOIN
courses_schedule ON courses_schedule.courses_schedule_uuid = CT.courses_schedule_uuid
LEFT JOIN
matrix_courses matrix_courses ON matrix_courses.courses_uuid = courses_schedule.courses_uuid
LEFT JOIN
roles_courses ON roles_courses.courses_uuid = matrix_courses.courses_uuid
LEFT JOIN
matrix M ON M.matrix_uuid = matrix_courses.matrix_uuid
LEFT JOIN
courses C ON C.courses_uuid = matrix_courses.courses_uuid
WHERE
C.courses_uuid = courses.courses_uuid
AND M.matrix_uuid = matrix.matrix_uuid
GROUP BY matrix.matrix_uuid , courses.courses_uuid)
AND courses_taken.overall_status = 1
AND employees.status = 2
AND IFNULL(employees.STATUS, 1) != 0
GROUP BY matrix.matrix_uuid , courses.courses_uuid , employees.employees_uuid
I have bought two courses VLSI electronics and VLSI extc which were
bought together so it has same order id
The query returns result when we searched for vlsi or electronics but
doesn't result anything when search in the filterextc.
SELECT `i.*,r.emailid,r.first_name,u.university_name,ct.country_name,st.state_name, cy.city_name,r.address,r.mobile,COUNT( k.order_id ) as order_cnt, k.product_type AS product_type,k.product_id AS product_id,tx.tax_amount,str.stream_name,fie.field_name,sems.semester_number,colg.college_name,`
IF( product_type = "course", (SELECT course_title FROM tbl_courses WHERE course_id = product_id AND course_title Like "%vlsi%"), 0 ) AS coursebought,
IF(product_type = "module",(SELECT module_title FROM tbl_modules WHERE module_id = product_id ), 0 ) AS modulebought
FROM (`tbl_orders` AS i)
LEFT JOIN tbl_users AS r ON i.user_id = r.user_id
LEFT JOIN tbl_discount_history AS hd ON i.order_id = hd.order_id
AND i.discount_id = hd.discount_id
AND hd.status = "Active"
LEFT JOIN tbl_shopping_cart AS k ON i.order_id = k.order_id
LEFT JOIN tbl_university AS u ON u.university_id = r.university_id
LEFT JOIN tbl_country AS ct ON u.country_id = ct.country_id
LEFT JOIN tbl_states AS st ON u.state_id = st.state_id
LEFT JOIN tbl_city AS cy ON u.city_id = cy.city_id
LEFT JOIN tbl_finalorder_tax_details AS tx ON i.order_id = tx.order_id
LEFT JOIN tbl_stream AS str ON r.stream_id = str.stream_id
LEFT JOIN tbl_fields AS fie ON r.field_id = fie.field_id
LEFT JOIN tbl_semester_type AS sems ON r.semester_id = sems.semester_id
LEFT JOIN tbl_colleges AS colg ON r.college_id = colg.college_id
and ct.country_id = cy.city_country_id
and st.state_id = cy.city_state_id
WHERE tx.tax_id = 1 and r.status = "Active" And 1=1 AND product_id IS NOT NULL
AND product_type = "course"
GROUP BY order_id
having coursebought IS NOT NULL and modulebought IS NOT NULL
ORDER BY order_id desc
limit 0,10
I have the following query and would like to convert it to using a left outer join instead of a not in to see if it would run faster that way. It's currently taking this query about 40 seconds to run on our database. I'm not familiar enough with using outer joins for this type of thing to convert it myself.
select
c.contact_id as contact_id,
c.orgid as organization_id,
c.first_name as first_name,
c.last_name as last_name,
a.address_state as state
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
where a.address_state = 'OH'
and (c.orgid = 45 or c.orgid = 55)
and c.contact_id NOT IN (
select pc.contact_id
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
inner join cnx_contact_group_participant as gp on c.contact_id = gp.contact_id
inner join cnx_contact_participant_role as cr on gp.participant_role_uid = cr.participant_role_uid
inner join cnx_contact_group as cg on gp.group_uid = cg.group_uid
inner join cnx_contact_group_participant as pgp on cg.primary_participant_uid = pgp.participant_uid
inner join cnx_contact as pc on pgp.contact_id = pc.contact_id
where (c.orgid = 45 or c.orgid = 55)
and cr.name = 'Applicant'
);
select
c.columns
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
LEFT JOIN
(Subquery goes here) x
ON x.contact _id = c.contact_id
where a.participant_state = 'OH'
and c.orgid IN(45,55)
and x.contact_id IS NULL;
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema