Jump to content

Recommended Posts

Hey,

 

I have a query which lists users in hospital and how much time they have left to wait based on "now" and "Arrival" time stamps.... but it works until i a WHERE and ORDER BY clause.

 

Im trying to only display people who have 1 minute left or greater in the WHERE CLAUSE

 

And ORDER BY minutes left in an ASC order but when i add them my query fails to find anything... this is my query:

 

 

<?php
SELECT UserID,Reason,TIMESTAMPDIFF(MINUTE,'Arrival','".serverdate()."')AS MinutesPassed,Minutes

FROM ".tablename().".hospital 

WHERE (TIMESTAMPDIFF(MINUTE,'Arrival','".serverdate()."')- Minutes) > 0 

ORDER BY (TIMESTAMPDIFF(MINUTE,'Arrival','".serverdate()."')- Minutes) ASC
?>

 

Hope you can help with my problem...

Time stamps and serverdate function are informat of: yyyy-mm-dd hh:mm:ss

Link to comment
https://forums.phpfreaks.com/topic/196669-query-shows-no-results/
Share on other sites

Arrival is TimeStamp YYYY-MM-DD HH:mm:ss

 

2010-03-27 02:14:10   

 

It resulted in : 0 minutes!

 

Which would be correct as 0 minutes had passed from when i inserted the row.

 

But now lets say field : Minutes is 10... that means they have to 10 till the result > or = to Minutes which is where my WHERE Clause comes in, i want to check where "how long they've been waiting from row insert" taken from "mintes - how long they have to wait" and find only where its > 0 minutes left.

 

Then order by minutes left ASC order but it finds nothing with my WHERE and ORDER part.

Ok so out of curiosity what does the following return?

 

SELECT TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) as MinutesPassed, Minutes
FROM hospital
WHERE TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) - Minutes > 0
ORDER BY MinutesPassed

 

if that returns nothing, then see what this returns

 

SELECT TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) as MinutesPassed, TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) - Minutes as MinutesLeft
FROM hospital
ORDER BY MinutesPassed

With the first query - where is "MinutesWaited" coming from as that would be undefined ?

 

The second query resulted in this:

<?php
echo $row['MinutesPassed'] .' passed  '. $row['MinutesLeft'].' left ';
?>

0 passed  -10 left 

 

Yeah sorry that should have been MinutesPassed.  But I also noticed that your Minutes left math is backwards. try this.

SELECT TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) as MinutesPassed, Minutes - TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) as MinutesLeft
FROM hospital
ORDER BY MinutesPassed

Okay thats better! Although the ORDERBY is last part not working.

 

As people can be in different lengths of waiting times other than just the same wait..

 

Person 1 has  2 minutes have passed and was a 20 minute wait = 18 mins to go.

Person 2 has  2 minutes have and was a 10 minute wait = 8 mins to go.

 

So person 2 would be before person 1 in terms of the order by. Im thinking a subtraction but can maths be used in an orderby?

Okay that worked but the WHERE clause is missing yet for some reason with this:

 

<?

SELECT TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) as MinutesPassed, Minutes - TIMESTAMPDIFF(MINUTE, `arrival` , NOW() ) as MinutesLeft
FROM hospital
WHERE MinutesLeft > 0 
ORDER BY MinutesLeft ASC

?>

 

I get : Unknown column 'MinutesLeft' in 'where clause'

Makes no sense as without it - the ORDER BY doesn't give an unknown column error :S So it must be setting but not useable in a WHERE ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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