vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Crazy SQL scripts involving DATELINE and DATE() (https://vborg.vbsupport.ru/showthread.php?t=48118)

Hwulex 01-25-2003 12:26 PM

Crazy SQL scripts involving DATELINE and DATE()
 
Ok.
I'm in the process of writing a new hack, and I'm hoping to include a 'member of the month' function to it. I've been thinking of different ways to do this (preferably without Cron Jobs), and thought I'd come up with a solution, but I'm having problems.
The motm is determined by a condition attached to each post.

Basically, the way I thought I'd do it is (pseudocode):
Code:

if (currentmonth != storedmonth)
  query_first(
    SELECT username, userid
    FROM user
    WHERE dateline <= todaysdate
    AND dateline >= a month ago
    ORDER BY condition
    DESC
  )
}

Basically, I need a way (within MySQL) of retrieving ONLY the posts that were made in the last month. Now, I thought I could embed the date() function within it, but that doesn't work.

Anyone any ideas on how I can do this?

As a temp mock-up, I've used:
Code:

    WHERE p.dateline >= $today-(60*60*24*30)
    AND p.dateline <= $today-(60*60*24)

But that's not terribly accurate, depending on when it gets executed, and the fact that not every month has 30 days etc.

I did try:
Code:

    WHERE ".date('mY',."p.dateline".)." = $currentmonth
But that throws out a parse error.


I hope this makes some sort of sense. I know I've been a little vague, but I don't wish to give too much away at this point. But if people need more info I will do my best to comply :)

Hwu

Icheb 01-25-2003 07:00 PM

First, get the number of the current month with PHP's date, then get the UNIX time of the first day in the current month and query the table for all entries which have a higher value in the time column.
There you go :-) .


PHP Code:

WHERE ".date('mY',."p.dateline".)." $currentmonth 

The reason why this doesn't work is the fact that p.dateline is no valid timestamp. Even it were, it wouldn't work since the query gets parsed by PHP and then it's being sent to the MySQL server, which returns the values for the queried fields. But date() needs the timestamp *before* the query is sent.
And btw, it doesn't work for a second reason: The value to the left has to be the name of a column, but i doubt date() will return the name of an already existing column.

Hwulex 01-26-2003 02:21 AM

Thanks for the suggestion, but I still don't think that will work, for the same reason I post above. AFAIC tell, that's the same logic as I was trying to use first time.
The problem is, I need to retrieve all posts from the previous month to this one, and I can't see a short way of doing it without much code writing.


I understand what you're saying about the quoted code. I never expected it to work, was just a wild stab in the dark out of desperation.
Btw, the p.dateline that you refer to is valid, but I haven't quoted the rest of the query. p is an alias for the post table which is set earlier in the query.

Cheers.

Icheb 01-26-2003 08:23 AM

I never said that p.dateline isn't valid. What i said was that the where-clause is not valid. You have to supply the name of a column there, but date() sure won't give you the name of an existing column.

Hwulex 01-26-2003 10:49 AM

Ah right, I see what you mean.
Sorry :(

Looks like I might have to forget this without cron jobs. Shame :(

Icheb 01-26-2003 01:19 PM

Why shouldn't this work? Just try it!

"First, get the number of the current month with PHP's date, then get the UNIX time of the first day in the current month and query the table for all entries which have a higher value in the time column."

Hwulex 01-27-2003 05:58 PM

Quote:

Originally posted by Icheb
Why shouldn't this work? Just try it!

"First, get the number of the current month with PHP's date, then get the UNIX time of the first day in the current month and query the table for all entries which have a higher value in the time column."

Hi Icheb.
Thanks for your pointers. I didn't quite follow what you were getting at the first time I read it, but looked again last night with a clear head, did some research, and have solved the problem.

Code looks something like this:
PHP Code:

// Member of the month stuff
$thismonth mktime (0,0,0,date("m"),1,  date("Y"));
$lastmonth mktime (0,0,0,date("m")-1,1,  date("Y"));
$motm $DB_site->query_first("
    SELECT
        u.userid,
        u.username
    FROM post p
    LEFT JOIN user u ON (p.userid = u.userid)
    LEFT JOIN usergroup g ON (u.usergroupid=g.usergroupid)
    WHERE p.dateline >= 
$lastmonth
    AND p.dateline <= 
$thismonth
    GROUP BY u.userid
    ORDER BY condition
    DESC
"
); 

Cheers.


All times are GMT. The time now is 07:05 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01049 seconds
  • Memory Usage 1,741KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete