Mysql Update select statement - mysql

I am trying to run an update on the following select statement but i dont think mysql is very fond of my syntax, Please advise?
UPDATE `invoice_lines`
SET
`invoice_lines`.`cost_price` = Costnew,
`invoice_lines`.`list_price` = Listnew,
`invoice_lines`.`unit_price` = Unitnew
SELECT (quote_lines.`list_price` * products.commision_pers/100) AS Listnew,
(quote_lines.`cost_price` * products.commision_pers/100) AS Costnew,
(quote_lines.`unit_price` * products.commision_pers/100) AS Unitnew
FROM `quote_lines`
INNER JOIN quotes
ON quotes.id = quote_lines.`quote_id`
INNER JOIN QuotePers
ON quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.`from_quote_id` = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.`invoice_id` = invoice.id
WHERE products.id = invoice_lines.`related_id`
AND prodid = invoice_lines.related_id
AND invoice_id = invoice.id
GROUP BY quotes.id,products.`id`

Buddy, Try to refer the table names from which those columns come from in where condition and next time you need to make use of table_name aliases while using such big queries :)
UPDATE invoice_lines
SET
invoice_lines.cost_price = Costnew,
invoice_lines.list_price = Listnew,
invoice_lines.unit_price = Unitnew
SELECT (quote_lines.list_price * products.commision_pers/100) AS Listnew,
(quote_lines.cost_price * products.commision_pers/100) AS Costnew,
(quote_lines.unit_price * products.commision_pers/100) AS Unitnew
FROM quote_lines
INNER JOIN quotes
ON quotes.id = quote_lines.quote_id
INNER JOIN QuotePers
ON quote_lines.quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.from_quote_id = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.invoice_id = invoice.id
WHERE products.id = invoice_lines.related_id
AND quote_lines.prodid = invoice_lines.related_id
AND quote_lines.invoice_id = invoice.id
GROUP BY quotes.id,products.id

Try this, I can not test it as I don't have your database schema:
UPDATE `invoice_lines`
INNER JOIN quotes
ON quotes.id = quote_lines.`quote_id`
INNER JOIN QuotePers
ON quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.`from_quote_id` = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.`invoice_id` = invoice.id
WHERE products.id = invoice_lines.`related_id`
AND prodid = invoice_lines.related_id
AND invoice_id = invoice.id
SET
`invoice_lines`.`cost_price` = (quote_lines.`cost_price` * products.commision_pers/100),
`invoice_lines`.`list_price` = (quote_lines.`list_price` * products.commision_pers/100),
`invoice_lines`.`unit_price` = (quote_lines.`unit_price` * products.commision_pers/100)
Hope it gets you on the right track.
Also check this related question

Related

FIND_IN_SET is getting wrong results

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

Mysql Fetching rows base on date

Here's the code:
SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
ip.dblItemPAmount, MAX(ip.dtmItemPasOf) AS EXR
FROM TBLREQUESTDETAILS RD,tblitem I,tblvendor v,tblitemunit iu,tblitemprice ip`
WHERE RD.strReqDItemCode = I.strItemCode
AND RD.strReqDItemUnitCode = iu.strItemUnitCode
AND RD.strReqDVendCode = v.strVendCode
AND i.strItemCode = ip.strItemPItemCode
and RD.strReqDReqHCode = 'RQST121'
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName, ip.dblItemPAmount
ORDER BY EXR desc ;
AND Here's The result:
What should I do If I want to fetch the current price for each itemname,vendorname and itemunit?? I want to fetch only those rows who's price is the Latest... Help me please those with boxes are the rows that i want to fetch
You can use where in the grouped value (and use explicict join notatio)
SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
ip.dblItemPAmount, ip.dtmItemPasOf AS EXR
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode
WHERE RD.strReqDReqHCode = 'RQST121'
and ( RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName, ip.dtmItemPasOf)
in ( SELECT RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,
MAX(ip.dtmItemPasOf)
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode
WHERE RD.strReqDReqHCode = 'RQST121'
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName
)
ORDER BY EXR desc ;
(and use explicict join notation .. i think is more readable)

How to join a table with a result field of a condition in MySQL

I have an SQL query where in my SELECT part I have a column which is a result of an IF statement. As you can see 'final_vendor_id' contains the value of ioproductrel.vendorid except when it's value is null or 0, because in this case the value of products.vendor_id is used:
SELECT
IF(ioproductrel.vendorid IS NULL OR ioproductrel.vendorid = 0, products.vendor_id, ioproductrel.vendorid) AS 'final_vendor_id',
account.accountname AS 'account',
salesorder.duedate
FROM
internalorder LEFT JOIN ioproductrel ON internalorder.internalorderid = ioproductrel.internalorderid
LEFT JOIN products ON ioproductrel.productid = products.productid
LEFT JOIN slip_relations ON internalorder.internalorderid = slip_relations.child_crmid
INNER JOIN salesorder ON slip_relations.parent_crmid = salesorder.salesorderid
LEFT JOIN account ON salesorder.accountid = account.accountid
LEFT JOIN account AS acc2 ON acc2.accountid = final_vendor_id
WHERE
internalorder.internalorderid = 8982
I want to join a table based on this 'final_vendor_id', but I just get an error that no field with such a name exists.
If I write the whole IF statement in the JOIN part again, it seems to work but Im not sure if it is safe and Im wondering if is there any simplier way than this:
SELECT
IF(ioproductrel.vendorid IS NULL OR ioproductrel.vendorid = 0, products.vendor_id, ioproductrel.vendorid) AS 'final_vendor_id',
account.accountname AS 'account',
salesorder.duedate
FROM
internalorder LEFT JOIN ioproductrel ON internalorder.internalorderid = ioproductrel.internalorderid
LEFT JOIN products ON ioproductrel.productid = products.productid
LEFT JOIN slip_relations ON internalorder.internalorderid = slip_relations.child_crmid
INNER JOIN salesorder ON slip_relations.parent_crmid = salesorder.salesorderid
LEFT JOIN account ON salesorder.accountid = account.accountid
LEFT JOIN account AS acc2 ON acc2.accountid = IF(ioproductrel.vendorid IS NULL OR ioproductrel.vendorid = 0, products.vendor_id, ioproductrel.vendorid)
WHERE
internalorder.internalorderid = 8982
What if set single quotes in left join section like:
...ON acc2.accountid = 'final_vendor_id'

MySQL Subquery check

I have a question.
This is my query:
SELECT
branches.naam as branche_naam,
subbranches.naam as subbranche_naam,
specialiteiten.naam as specialiteiten_naam
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = company.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE bedrijfgegevens.id in
(SELECT
bedrijfgegevens.id FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "test"
AND subbranches.naam = "demo"
AND specialiteiten.naam = "bla")
I think it's way to long and I am using the same tables.
What it does is:
subquery: get the company id where "specialiteiten.naam" = "bla".
query: get the other "specialiteiten" from that "bedrijf"
One method is to use a view.
If you want the values in a single delimited list, you can use group_concat():
SELECT bedrijfgegevens.id,
GROUP_CONCAT(CONCAT_WS(':', branches.naam, subbranches.naam, specialiteiten.naam))
FROM bedrijfgegevens INNER JOIN
subbranches
on subbranches.id = company.subbranche_id INNER JOIN
branches
on branches.id = subbranches.branche_id INNER JOIN
bedrijfgegevens_specialiteiten
on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id INNER JOIN
specialiteiten
on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
GROUP BY bedrijfgegevens.id
HAVING SUM(branches.naam = 'test' AND subbranches.naam = 'demo' AND specialiteiten.naam = 'bla') > 0;

slow query with joins

Please am having difficulty in optimizing this query. What am trying to achieve is to join about 8 tables, of which only about 3 of the tables contains large data (1.5m records). This query returns expected records but is taking 1min to run which is bad.
I know it can be optimized to perform far better, pls i need assistance from you experts. I have index on the fields used for join already.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM (_sch_forum_topics
INNER JOIN _users
ON ( _users.userid = _sch_forum_topics.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid)
WHERE _sch_forum_topics.sch_sub_forum_id = 4
ORDER BY _sch_forum_topics.topic_last_post_time DESC
LIMIT 0, 15
Try to filter before making JOIN's.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM
( select * FROM sch_forum_topics WHERE sch_sub_forum_id = 4
ORDER BY topic_last_post_time DESC
LIMIT 0, 15 ) main
INNER JOIN _users
ON ( _users.userid = main.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid);