I have a MySQL query that's using SUM() to take the sum of a column, BUT what I need it to do is basically this:

If the string in another column is 'BUY', then I want to add the number in my column, but if this other column has 'SEL', I want to subtract the number in my column. I'm not sure how to go about this.

Any help ASAP would be appreciated. Thanks. :confused:

Recommended Answers

All 2 Replies

Use an IF clause, like in

select sum(amount * if(ordertype='BUY',1,-1)) from mytable

If this does not suit your needs, submit a test case.

I have a MySQL query that's using SUM() to take the sum of a column, BUT what I need it to do is basically this:

If the string in another column is 'BUY', then I want to add the number in my column, but if this other column has 'SEL', I want to subtract the number in my column. I'm not sure how to go about this.

Any help ASAP would be appreciated. Thanks. :confused:

Nevermind. What I needed was slightly different and was even a simpler fix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.