andrisp Posted June 9, 2006 Report Share Posted June 9, 2006 Ir divas tabulas - vienaa glabaajaas ieraksti par materiaaliem, otraa - informaacija par iepirkshanas un izlietoshanas operaacijaam shiem pashiem materiaaliem. Struktuura apt. shaada: (Te ir iistaa structuura) MATERIALI id | name | critical_quantity OPERACIJAS id | materiala_id | operation_type | amount //operation_type - enum('in', 'out'); Meegjinu uzrakstiit sql pieprasiijumu, kas sasumme attieciigaa materiala_id visas ' amount' veertiibas, kur operation_type ir 'in', un arii attieciigi sasummee operation_type = 'out' operaacijas attieciigam materiala_id. Peec tam atnjem shiis veertiibas un saliidzina ar critical_quantity no MATERIALI. Ja ir mazaaks, tad atgriezh rezultaa. Pats esmu izburtojis kaut ko shaadu, bet negrib straadaat: SELECT m.id, m.name, m.critical_quantity FROM materiali_operacijas as i, materiali_operacijas as o, materiali as m WHERE i.operation_type = 'in' AND o.operation_type = 'out' AND m.id = i.materiala_id AND m.id = o.materiala_id HAVING m.critical_quantity <= (SUM(i.amount) - SUM(o.amount)) Paliidzeesiet, luudzu ? Link to comment Share on other sites More sharing options...
bubu Posted June 9, 2006 Report Share Posted June 9, 2006 SELECT m.id, m.name, m.critical_quantity FROM materiali as m JOIN (SELECT materiala_id , SUM(IF(operation_type="in", +1, -1)*amount) as amount FROM operacijas GROUP BY materiala_id) as ops ON m.id = ops.materiala_id WHERE m.critical_quantity <= ops.amount Link to comment Share on other sites More sharing options...
andrisp Posted June 9, 2006 Author Report Share Posted June 9, 2006 (edited) Paldies, bubu, straadaa Zheel, ka es shitaadu nevareeju izdomaat :) Edited June 9, 2006 by andrisp Link to comment Share on other sites More sharing options...
andrisp Posted June 9, 2006 Author Report Share Posted June 9, 2006 Tomeer kaut kas nav labi - lielaakai daljai ierakstu ops.amount sanaak 18446744073709552016 un liidziigi skaitlji. Link to comment Share on other sites More sharing options...
bubu Posted June 9, 2006 Report Share Posted June 9, 2006 Tāpēc, ka amount ir unsigned tips. Kā unsigned var būt -1? Link to comment Share on other sites More sharing options...
Recommended Posts