I have a little trouble with my subquery. I try to only take the nearest event in a table with a min value, but I need it to be confirmed (confirmed = 1 and cancelled = 0)
There's my table :
uid | location | date | open | close | confirmed |
1 | aaa | 2015-05-11 | 12:00 | 14:30 | 1 |
2 | aaa | 2015-05-12 | 15:00 | 22:30 | 0 |
3 | aaa | 2015-05-15 | 11:00 | 18:30 | 0 |
4 | aaa | 2015-05-16 | 09:00 | 16:00 | 1 |
And there is my query : ( $now
is a php date of current hour (H:i) and $today
a php date in format Ymd)
SELECT location, MIN(date)
FROM table
WHERE confirmed = 1
AND IF (
(close > '$now' AND date = '$today') OR (date> '$today'), 1, 0
)
GROUP By location
So in the test format, we are May 11 at 17:00. The first one shouldn't be selected because it happened in the past (this morning) and the second one should not be selected too because i put confirmed to 0
The expected result is
aaa | 2015-05-16
because its the nearest with the confirm to true,
but it always returns me
aaa | 2015-05-12
It doesn't care about the where statement of confirmed = 1, but it cares about the if (close > '$now' AND date = '$today') OR (date> '$today'), 1, 0 because it returns me the second one.
Can someone provide me some help please?
PS: It's all in MySQL
Thank you.
-- Carlos
EDIT :::
I just tried to add some value in the select (SELECT *, MIN(date))
and it returns me asymetric data ???
I got
| 4 | aaa | 2015-05-16 | 09:00 | 16:00 | 1 | 2015-05-12
So the first date is the good one and the second one (with the min()) isn't the good one.
LAST EDIT :::
AS suggested spencer7593 (thanks to him/her), i just removed the min(date) and it automatically returns me the data I want, the good sql is :
SELECT *
FROM table
WHERE confirmed = 1
AND IF (
(close > '$now' AND date = '$today') OR (date> '$today'), 1, 0
)
GROUP By location
Try like this:
SELECT location, MIN(date)
FROM table
WHERE confirmed = 1
AND ((close > '$now' AND date = '$today') OR (date> '$today'))
GROUP By location
Maybe the If statement is making the behavior wrong.
Given the example data, it certainly doesn't look like the date value of 2015-05-12
should be returned.
I'm questioning what the datatypes of the columns are, the confirmed
column in particular. If that column is CHAR
or VARCHAR
, perhaps the comparison is being done as character, rather than numeric, and there's some hidden characters that's making it unequal.
If that's the case, and it's trailing characters in the column value, you can try casting confirmed
to numeric by adding a zero to it in the statement, eg
WHERE confirmed+0 = 1
That expression confirmed+0
will be evaluated in numeric context.
I'm also suspicious of the datatypes of the date
and close
columns, if those are also VARCHAR or CHAR... though that wouldn't explain why the predicate on confirmed
is being ignored.
We're just guessing, there's not enough information to really tell what's happening.
For debugging this type of issue, echo
or vardump
the SQL text before it's sent to the database, so you can see what's actually being sent.
$sql = "SELECT .... " . $var . " ... ORDER BY 1";
echo $sql; # for debugging
$dbh->query($sql);
I'd take that SQL text thats emitted, and test in another client. I'd try removing the MIN
aggregate function and the GROUP BY
, and return all of the relevant columns from the rows, to see the actual rows that are being grouped.
Assuming you are trying to find the location from the first confirmed record after the supplied date and time.
SELECT `location`, `date`
FROM `table`
WHERE `confirmed` = 1
AND (`date` > '$today'
OR (`date` = '$today' AND `close` > '$now')
)
ORDER BY `date`, `close`
LIMIT 1
;
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.