Convert sql code to work on VanillaModel
I"m try to develop new plugin that display top five thanks received uses on last 7 days..here working code that give me expect result
SELECT COUNT(vf_ThanksLog.DateInserted) AS Thanks,vf_ThanksLog.UserID,vf_User.Name FROM vf_ThanksLog LEFT JOIN vf_User ON vf_ThanksLog.UserID=vf_User.UserID WHERE vf_ThanksLog.DateInserted BETWEEN NOW()-INTERVAL 6 DAY AND NOW() GROUP BY `UserID` ORDER BY thanks DESC LIMIT 05;
Now i want convert it work on VanillaModel..here my code
SQL ->Select('u.UserID, u.Name,u.Email, u.Photo') ->Select( 'tc.DateInserted','count','ThankCount') ->From('ThanksLog tc') ->Join('User u', 'tc.UserID = u.UserID') ->WHERE('tc.DateInserted BETWEEN NOW()-INTERVAL 6 DAY AND NOW()') ->GroupBy('u.UserID') ->Limit("$limit") ->OrderBy('ThankCount', "Desc") ->Get() ->ResultArray();
It's given me some result,but thier where code not working as expect
when debug
select u.UserID as `UserID`, u.Name as `Name`, u.Email as `Email`, u.Photo as `Photo`, count(tc.DateInserted) as `ThankCount` from vf_ThanksLog tc join vf_User u on tc.UserID = u.UserID where tc.DateInserted BETWEEN NOW()-INTERVAL 6 DAY AND NOW() is null group by u.UserID order by ThankCount desc limit 5;
so problem is how to remove is null on where code
where tc.DateInserted BETWEEN NOW()-INTERVAL 6 DAY AND NOW() is null
please help me..any solution ?
Tagged:
0
Comments
The
Where()
method takes two arguments:SomeField
,SomeField =
If you leave the second argument empty, you are saying "compare field
tc.DateInserted BETWEEN NOW()-INTERVAL 6 DAY AND NOW()
with null".I'm not 100% sure it will work, but have you tried this?
My shop | About Me
How about:
grep is your friend.
That would definitely be better.
My shop | About Me
you're the sql nut, is there any difference at in performance?
grep is your friend.
when i used it, it's given me vanilla Bonk error page
Thanks for help friend ,those additional details help me to improve knowledge
That's the head of the Where function:
If it is not already working with the solution businessdad has provided, you can try
where('tc.DateInserted', 'BETWEEN NOW()-INTERVAL 6 DAY AND NOW()', true, false)
Debug result
As I wrote, I wasn't sure it would work (support for the
BETWEEN
clause is still a bit dodgy in Vanilla framework). You can use the example given by @x00, which should work.Universal DBA answer: it depends.
Practical answer: in most RDBMS, the
BETWEEN
clause is transformed in two AND conditions by the engine, it just serves as a handy shortcut. I'm not aware of significant differences in query plans when usingBETWEEN
or two separateWHERE
clauses.Ironically, when dealing with dates, the
BETWEEN
is often useless, as a it includes the specified values, while, with dates, you usually want to exclude the last one.That is:
WHERE MyField BETWEEN '20140101' AND '20140201'
will retrieve the data from the 1st of January to 2nd of February included.WHERE (MyField >= '20140101') AND (MyField < '20140201')
will retrieve the data from the 1st of January included to the 31st of January (i.e. the 2nd of February is excluded, as it should be).@x00 I'm sure you are aware of that, I just added it for anyone who might not be aware of the difference.
My shop | About Me
debug code
WHERE tc.DateInserted > 'NOW()-INTERVAL 6 DAY' AND tc.DateInserted < 'NOW()'
0 row(s) returned
test that code directly phpmyadmin after modify as below it's given me correct result
WHERE tc.DateInserted > NOW()-INTERVAL 6 DAY AND tc.DateInserted < NOW()
any help?
when i used it, it's given me vanilla Bonk error page
Yes, I've tried the following (had to change the table though):
and got
which is really strange...
But at least the where clause isn't quoted any more!
see if this helps.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.
finally at last it's work..thank u very much
the pleasure is mine.
I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.