-1

I'm trying to do a coupled OR statement

select * from table where cat = "x" OR (cat = "y" AND rand()<=0.25);

So I would like to select all items with cat = x or cat = y, but only 25% of y. The statement above gives med 25% of both x and y.

How can I seperate my statement so this can be done?

Thanks

user2102266
  • 539
  • 3
  • 14
12344343243
  • 43
  • 1
  • 10
  • 2
    What do you mean with "but only 25% of y"? – fancyPants Jul 11 '17 at 09:12
  • You can not do this with ORs if cat="x" then it will not even try to look at Y. That gives u all Xs and quarter of Ys – user2102266 Jul 11 '17 at 11:50
  • Odd behavior. Perhaps instead of OR use a union ? I'd like to see this behavior in action in a sqlfiddle.com or rextest.com example. as all of x should be returned adn only y if a random number is less than 25% note: you're not guaranteed 25% of y. all you're guaranteed is that if a random value between 0 and 1 is <=.25 you'll get that record. – xQbert Jul 11 '17 at 14:50

1 Answers1

0

I don't know what you are doing but i can almost smell that this is a very dodgy way to implement SQL.

I assume, you want to change your where clause if random number generates a result with %25 probability.You can not do this with ORs if cat="x" then it will not even try to look at Y. That gives u all Xs and quarter of Ys.

Try this for implementation of probablity check.

IF  (RAND() <= 0.25)  
     { sql_statement_forY  }   
[ ELSE   
     { sql_statement_forX  } ]  
user2102266
  • 539
  • 3
  • 14