Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:17:36+00:00 2026-05-30T15:17:36+00:00

Basically I store data in MySql 5.5. I use qt to connect to mysql.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:17:38+00:00Added an answer on May 30, 2026 at 3:17 pm

    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 someTable but the principle is sound.

    SELECT COUNT(*) 
    FROM (
        SELECT
            @multiplier:=@multiplier*IF(t.`col1`<t.`col2`,0,1) AS counter
        FROM `someTable` t, (SELECT @multiplier := 1) v
        HAVING counter = 1
    ) scanQuery
    

    The @multiplier variable will keep multiplying itself by 1. When it encounters a row where col1 < col2 it 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

    SELECT 
        `someTable`.*
    FROM `someTable`
    INNER JOIN (
        SELECT
            `someTable`.`PrimaryKeyField`
            @multiplier:=@multiplier*IF(`col1`<`col2`,0,1) AS counter
        FROM `someTable` t, (SELECT @multiplier := 1) v
        HAVING counter = 1
    ) t
    ON scanQuery.`PrimaryKeyField` = `someTable`.`PrimaryKeyField`
    

    Or possibly simply

        SELECT
            `someTable`.*
            @multiplier:=@multiplier*IF(`col1`<`col2`,0,1) AS counter
        FROM `someTable` t, (SELECT @multiplier := 1) v
        HAVING counter = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting to use mysql, and I'm basically using it to store and
I need a way to store an int for N columns. Basically what I
Basically, I have multiple URL's stored in a MySQL table. I want to pull
I have two MySQL queries in my PHP code. Basically I need the two
I currently have a MySQL database which I was hoping to use to store
I'm trying to use some mysql data in creating part of a variable name
I'm making a Stored Procedure that basically concatenates a long string with data from
I am trying to store metadata (basically a unique id) along with each cell
Basically I would like to store the address of a pointer in a buffer.
I have some small sets of data from the database (mysql) who are seldom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.