简体   繁体   中英

How would I count MYSQL rows down to a specific row in PHP?

I have a table that acts as a dropbox for files that get automatically queued.

I order the priority of files based on two things, the date and time, and if it has been verified or not.

Here is my query:

$dropcount = mysql_query("SELECT COUNT(*) FROM queue WHERE status=2 OR status=3 
OR status=4 ORDER BY authorised DESC, timestamp DESC;");

What I need to do is count how many rows are ahead of the users current file because this will count every row, including the files behind the current users as well.

Thank you.

My table looks like this:

http://tabbidesign.com/table.png

The way this works is if a file is added its added to the queue. However if that file is authorised, it take priority over non verified files which means it needs to be counted even though it was added after some of the non verified flies.

The status column states if the file is: -Being processed

-Queueing in the dropbox

-Cancelled by the user

-Cancelled by the admin

EDIT: Ok @curtisdf's answer would work if I did the following:

$countauth =
SELECT COUNT(*)
FROM queue
WHERE (status=2 OR status=3 OR status=4)
AND authorised=1
AND timestamp < $timestamp

$countnorm =
SELECT COUNT(*)
FROM queue
WHERE (status=2 OR status=3 OR status=4)
AND authorised=0
AND timestamp < $timestamp

$actualcount = $countnorm + $countauth;

How could I merge this into one query, or is this not possible?

THE SOLOUTION

SELECT COUNT(*)FROM queue WHERE (status=2 OR status=3 OR status=4)AND 
timestamp < $timestamp OR authorised=1

Found by @curtisdf.

Assuming your idqueue column is the primary key and it's auto-incremented, what about using it to filter out later ID's? Something like:

SELECT COUNT(*) 
FROM queue 
WHERE status=2 OR status=3 OR status=4 
AND idqueue < $currentFileId
ORDER BY authorised DESC, timestamp DESC

EDIT: It's a little difficult to know how to answer because your table has so many columns and it's not clear what you're using all of them for. What does the status column mean, for example, in contrast to the authorized column? But anyway, since your ORDER BY doesn't matter for this query, and since you're looking for authorised files that came in prior to a given file, how about this:

SELECT COUNT(*)
FROM queue
WHERE (status=2 OR status=3 OR status=4)
AND authorised=1
AND timestamp < $timestamp

EDIT 2: Okay, now I see what you're after. :-) If you just removed the "AND authorised=X" parts, I believe you could do it in one query. This assumes your authorised column only would have values of 0 or 1 . But if it has more than that, then you could do it by wrapping the WHERE ... AND ... parts of each query in their own parentheses, like this:

SELECT COUNT(*)
FROM queue
WHERE (
    (status=2 OR status=3 OR status=4)
    AND authorised=1
    AND timestamp < $timestamp
) OR (
    (status=2 OR status=3 OR status=4)
    AND authorised=0
    AND timestamp < $timestamp
)

This could be simplified even further:

SELECT COUNT(*)
FROM queue
WHERE (status=2 OR status=3 OR status=4)
AND timestamp < $timestamp
AND (authorised=1 OR authorised=0)

You need to add in a where statement:

$dropcount = mysql_query("SELECT COUNT(*) FROM queue WHERE (status=2 OR status=3
OR status=4) AND authorised=1 and timestamp<'$currentUserTimeStamp'  ORDER BY 
authorised DESC, timestamp DESC;");

If you don't know the current user timestamp, you'll need to select that first.

$result = mysql_query("select `timestamp` from queue where (status=2 or status=3 or 
status=4) and `userid`='$user'");
$row = mysql_fetch_row($result);
$currentUserTimeStamp = $row[0];

Of course, I've made up the column name for the user id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM