Basically I store data in MySql 5.5. I use qt to connect to mysql. I want to compare two columns, if col1 is greater than col2, the count continues, but when col1 is less than col2, count finishes and exits. So this is to count how many rows under some condition at the beginning of column. Is it possible in mysql?
An example:
Col1 Col2
2 1
2 3
2 1
The count I need should return 1, because the first row meets the condition of Col1 > Col2, but the second row doesn’t. Whenever the condition is not meet, counting exits no matter if following rows meet the condition or not.
This is a bit ugly, but will do the job. It’ll need adjusting depending on any ORDER etc you would like to apply to
someTablebut the principle is sound.The @multiplier variable will keep multiplying itself by 1. When it encounters a row where
col1 < col2it multiplies by 0. It will then continue multiplying 0 x 1. The outer query then just sums them up.It’s not ideal, but would suffice. This could be expanded to allow you to get those rows before the break by doing the following
Or possibly simply