当前位置: 动力学知识库 > 问答 > 编程问答 >

php - 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.

网友答案:

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?

网友答案:

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!

网友答案:

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..

分享给朋友:
您可能感兴趣的文章:
随机阅读: