1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to fetch only odd rows from the mysql?

Discussion in 'MySQL' started by sunbros, Oct 29, 2010.

  1. #1
    I have a strange query, How to fetch only odd rows from the mysql? Can anyone help me?
     
    sunbros, Oct 29, 2010 IP
  2. seotraining

    seotraining Active Member

    Messages:
    511
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    It depends on what type of data you are having if you have auto increment field in it then you might have unique number and you can find the query using php script
     
    seotraining, Oct 30, 2010 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    This can only be done if you have a numerical field in your database upon which you are using to determine if a record is odd/even. Let's call this 'FieldN'. The below will be the WHERE clause of your query and will return only those records where that number is odd:

    'WHERE MOD([FieldN],2)=1'

    For more info about the MOD function you can go to this link:
    http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod
     
    plog, Nov 1, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    If you want odd rows, it's actually just a little faster to use a bitwise AND than it is to use MOD.

    SELECT * FROM `table` WHERE id & 1;
    Code (markup):
    On a table with 2 million rows, it takes 1.43 seconds to count all of the odd rows with the AND operator.
    Whereas with the MOD function it takes 1.63 seconds to count them.
    Using the modulo operator (WHERE id % 2) is just a little faster than the MOD function, but still slower than the bitwise AND.
     
    joebert, Nov 6, 2010 IP
  5. teamaguilar

    teamaguilar Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks guys :) a quick way to fetch the odd rows
     
    teamaguilar, Nov 23, 2010 IP