Jump to content

Adding a WHERE to this query


ukscotth

Recommended Posts

Hi,

 

This is probably very easy for someone who knows how but I cant seem to get it working.

 

Can someone please tell me how I can alter this query to select a 'WHERE gender = female' so instead of picking a random user it picks a random female user ?

 

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$user1b = mysql_query( " SELECT * FROM `users` LIMIT $offset, 1 " );
$user1= mysql_fetch_array($user1b);

 

Also, would it be easy to modify it so it picks a second random user that isnt the same as the first user ? so I end up with 2 random females ( $user1 and $user2 )

 

Many thanks,

 

Scott

Link to comment
https://forums.phpfreaks.com/topic/250395-adding-a-where-to-this-query/
Share on other sites

  Quote

Can someone please tell me how I can alter this query to select a 'WHERE gender = female' so instead of picking a random user it picks a random female user ?

 

 

The WHERE clause would go after the FROM:

 

<?php
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE gender='female'");
//...
?>

  Quote

also need to add ' WHERE uid != $user' somehow.

 

Any ideas ?

 

 

Which query is that supposed to be added to? Note that the following description of the WHERE clause might be helpful:

http://dev.mysql.com/doc/refman/5.0/en/where-optimizations.html

No problem, glad to hear it's working.  8)

 

 

Based on the original post:

 

  Quote
...query to select a 'WHERE gender = female' so instead...

 

Maybe the first attempt didn't include the quotes around female... For example, this won't work:

 

<?php
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE gender=female");
//...
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.