Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem with sql query
Old 09-30-2011, 09:21 PM Problem with sql query
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
Hello guys i have a problem with a SQL query it counts the records per month. From october 2010 to august 2011 the record count is good but for some reason the first month sept 2010 it shows 99 records but that is not correct it should be only 15 records i don't know whats wrong with the query here it is:

PHP Code:
SELECT COUNT(`case_#`) AS CASESMONTH(`fecha_actual`) AS `Month`, YEAR(`fecha_actual`) AS `YearFROM `vital_info`
GROUP BY MONTH(`fecha_actual`)
ORDER BY (`fecha_actual`) 
Please any help I would really appreciate it. Thanks
stivens is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-30-2011, 10:21 PM Re: Problem with sql query
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
ok I just found the problem. The problem is that it is adding september 2010 and september 2011 now my question is, is there a way to select it by month and year so this would work? please any help would be appreciate it. Thanks
stivens is offline
Reply With Quote
View Public Profile
 
Old 10-01-2011, 05:19 AM Re: Problem with sql query
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Not sure if the DATE_SUB() function will help at all?
http://www.w3schools.com/sql/func_date_sub.asp
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 10-01-2011, 10:23 AM Re: Problem with sql query
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Try grouping by both the year and month, as in

"group by YEAR(`fecha_actual`), MONTH(`fecha_actual`)"

And since you've already created aliases for the year and month, you can use those instead, as in

"group by Year, Month"
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-04-2011, 01:32 AM Re: Problem with sql query
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
Thanks lizciz this actually worked great! now i have a question i need to display the last 12 months i put ORDER BY ('fecha_actual') DESC LIMIT 12 and it works fine but when it displays it i need to display in Ascending order is there a way to do this??
stivens is offline
Reply With Quote
View Public Profile
 
Old 10-04-2011, 06:08 AM Re: Problem with sql query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
change DESC (descending) to ASC (ascending)
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-04-2011, 12:52 PM Re: Problem with sql query
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
Quote:
Originally Posted by chrishirst View Post
change DESC (descending) to ASC (ascending)
this wouldn't work because it will show the first 12 on the DB but i need the last 12 order by DESC date from the DB and then display them in ASC order...
stivens is offline
Reply With Quote
View Public Profile
 
Old 10-04-2011, 01:53 PM Re: Problem with sql query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
set your "print" loop to work backwards.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Problem with sql query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26633 seconds with 12 queries