Sujit Tiwari
Sujit Tiwari

Reputation: 363

mysql query with two where clause

i am making auto scroll of members .i have two mysql query , i want to display the members who is having photo and ordered by last login . so in first set i am not having any problem as i am doing like this

$sql=mysql_query("SELECT * 
                    FROM accounts 
                   WHERE avatar != '' 
                ORDER BY lastlogin DESC 
                   LIMIT 50");

This is working fine. But as the user scroll and goes to the bottom of page i am trying to load next set of 50 users ordered by same filtration done above. So for that i am doing

$sql = mysql_query("SELECT * 
                      FROM accounts  
                     WHERE lastlogin < '$last_msg_id' 
                  ORDER BY lastlogin DESC 
                     LIMIT 50");

The above one is used to find the id of last login i.e id of 51th member from the last login. now i have to filter those members having photo:

$sql = mysql_query("SELECT * 
                      FROM accounts 
                     WHERE avatar != '' 
                  ORDER BY lastlogin DESC 
                     LIMIT 50");

Please tell how i can combine these two query as on i have no success.

Upvotes: 3

Views: 3846

Answers (3)

joelhardi
joelhardi

Reputation: 11179

I think what you're trying to do is just display the results of the first query (the list of users with avatars), but in blocks of 50 per page.

In that case, what you actually want to do is use LIMIT with 2 parameters. (See MySQL SELECT syntax for full details.)

When you use LIMIT with two parameters, the first is the offset to start at, the second is the maximum number of records. So, for example:

SELECT * FROM accounts LIMIT 0,50;   # Retrieves users 1-50
SELECT * FROM accounts LIMIT 50,50;  # Retrieves users 51-100
SELECT * FROM accounts LIMIT 100,50; # Retrieves users 101-150
                                     # etc.

If that's not what you mean to do, then T.J.'s answer (just add an AND to have two WHERE clauses) is the way to go!

Upvotes: 3

Pawan
Pawan

Reputation: 324

if you ever have any such queries you can always open phpmyadmin and try a search using the search tab ... this way your query is automatically generated error free..

Upvotes: -1

T.J. Schuck
T.J. Schuck

Reputation: 3645

If I'm understanding your question correctly, I think you just need a well-placed AND, like so:

  SELECT * 
    FROM accounts 
   WHERE lastlogin < '$last_msg_id' 
     AND avatar != '' 
ORDER BY lastlogin desc limit 50

Is that what you're trying to do?

Upvotes: 6

Related Questions