0

Possible Duplicate:
Best way to prevent a value from going negative in mysql

Guys I am new to here and also to the programming. I am working on an inventory control system. I'll explain my problem using an example. Think my current stock is 500 sales should be reduced from the stock. When I am entering sales amount if I enter 1000 value would be -500. How can i avoid this?? currently I am doing it like this

--php pseudo code---

if(stock > sales)
 { stock - sales}

else
 {error}

my data type is float(10,2).

Can I avoid this problem in any other way? I don't know whether my question is clear or not. (Sorry for my bad language skills)

4

3 に答える 3

3

あなたのアプローチは問題ないように見えますが、複数のスレッドが同時にデータを操作できる場合は、競合状態になる可能性があることに注意してください。分離レベルがシリアライズ可能に設定されたトランザクションを使用して、2つのスレッドが同時に値を減少させないようにすることができます。

列タイプunsigned intを作成して、負の値が含まれないようにすることもできますが、それでもこの条件を明示的に処理する必要があります。

于 2012-07-08T21:19:00.273 に答える
2

Mysqlでは、数値の列タイプをUNSIGNEDにすることができます。これは、基本的に負の列を指定できないことを意味します。それについてのリファレンスが見つからなかったので、これを試してみてください

于 2012-07-08T21:19:10.307 に答える
1

0 に設定された一時変数を作成し、実際に正の値になったときに値を設定できます。例:

$res=0;
if($stock > $sales)
 $res=$stock-$sales

または単に:

$res = $stock > $sales ? $stock - $sales: 0

このように $res が 0 未満になることはありません

于 2012-07-08T21:23:37.073 に答える