MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 8th, 2012, 02:27 PM
gmrstudios gmrstudios is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 119 gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 17 h 16 m 15 sec
Reputation Power: 7
MSSQL to MYSQL conversion

Hello and thanks for reading. I had to rewrite a query after moving from a MSSQL to MYSQL and wanted to see if my SYNTAX was correct. While it looks like it is pulling the same data I wanted to run it by this fine forum to see if I am using the proper functions, etc.

MSSQL
PHP Code:
 SELECT datepart(wk,modified_on) = datepart(wk,dateadd(d,-7,getdate())) 
AND 
year(modified_on) = year(dateadd(d,-7,getdate())) 


Converted to...

PHP Code:
 SELECT DATE_FORMAT'ada_date''%W' ) = DATE_FORMAT'date_sub(CURDATE(), INTERVAL 7 DAY)''%W' 
AND 
YEAR'ada_date' ) = YEAR'date_sub(CURDATE(), INTERVAL 7 DAY)' 

Last edited by gmrstudios : May 8th, 2012 at 02:30 PM.

Reply With Quote
  #2  
Old May 8th, 2012, 03:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,443 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 24 m 2 sec
Reputation Power: 4141
i have a problem understanding what your mssql query was doing

perhaps you copy/pasted it incorrectly?

Code:
SELECT datepart(wk,modified_on) = datepart(wk,dateadd(d,-7,getdate()))
why are you equating the two weeks?

are you hoping to get a TRUE/FALSE result?

does mssql return 0/1 instead?

in any case, you have some problems with your mysql functions

for example, here --
Code:
DATE_FORMAT( 'ada_date', '%W' )
you're trying to get the week number of a string

[sigh]... another developer is pwned by mysql's evil backticks

remove the single quotes
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old May 8th, 2012, 04:32 PM
gmrstudios gmrstudios is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 119 gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 17 h 16 m 15 sec
Reputation Power: 7
Ooops! My apologizes this is the actual query:

PHP Code:
 $result_lw=mysql_query("SELECT ada_date
FROM psc_addons
WHERE ada_date >= DATE_FORMAT('ada_date','%W') = DATE_FORMAT('date_sub(CURDATE(), INTERVAL 7 DAY)','%W') AND YEAR('ada_date') = YEAR('date_sub(CURDATE(), INTERVAL 7 DAY)')"
)
or die (
'Query failed: ' mysql_error() . '');
$num_rows_lw mysql_num_rows($result_lw); 


I am attempting to pull the total amount of records from the previous week. I then do a mysql_num_rows() on the result.

Reply With Quote
  #4  
Old May 8th, 2012, 05:15 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,443 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 24 m 2 sec
Reputation Power: 4141
Quote:
Originally Posted by gmrstudios
Ooops! My apologizes this is the actual query:
sorry, but i just don't believe it

you have this --

Code:
WHERE ada_date >= DATE_FORMAT('ada_date','%W') = DATE_FORMAT('date_sub(CURDATE(), INTERVAL 7 DAY)','%W') 
in other words, WHERE a = b = c

that's just plain invalid (well, actually, mysql will execute it, but it will always be false)

and by the way, you're still trying to get the week number of a string -- get rid of those quotes!

Reply With Quote
  #5  
Old May 8th, 2012, 06:51 PM
gmrstudios gmrstudios is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 119 gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 17 h 16 m 15 sec
Reputation Power: 7
Humm... So this was my original MSSQL query. It works just fine over in MSSQL land (I no longer need the status_lookup_id portion in the new query).

PHP Code:
 SELECT number     
FROM workitem
WHERE status_lookup_id 
600 
AND 
datepart(wk,modified_on) = datepart(wk,dateadd(d,-7,getdate())) 
AND 
year(modified_on) = year(dateadd(d,-7,getdate())) 


Which is how I came up with...

PHP Code:
 SELECT ada_date     
FROM psc_addons
WHERE
DATE_FORMAT
('ada_date','%W') = DATE_FORMAT('date_sub(CURDATE(), INTERVAL 7 DAY)','%W')
AND 
YEAR('ada_date') = YEAR('date_sub(CURDATE(), INTERVAL 7 DAY)'


When I remove the ' from the second query MySQL seems to get pissed. With the goal of all of this being to pull all records with a ada_date (2000-00-00 00:00:00 is the format) from the previous week.

Last edited by gmrstudios : May 8th, 2012 at 06:56 PM.

Reply With Quote
  #6  
Old May 8th, 2012, 07:28 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,443 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 24 m 2 sec
Reputation Power: 4141
can you understand my confusion? did you notice that the WHERE a = b = c disappeared in your last post?

let me try a third time to point out the problem

this --
Code:
DATE_FORMAT('ada_date','%W')
is trying to get the week number of a string

this --
Code:
DATE_FORMAT(ada_date,'%W')
gets the week number of a column

with me now?

Reply With Quote
  #7  
Old May 8th, 2012, 08:52 PM
gmrstudios gmrstudios is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 119 gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 17 h 16 m 15 sec
Reputation Power: 7
Quote:
Originally Posted by r937
can you understand my confusion? did you notice that the WHERE a = b = c disappeared in your last post?

let me try a third time to point out the problem

this --
Code:
DATE_FORMAT('ada_date','%W')
is trying to get the week number of a string

this --
Code:
DATE_FORMAT(ada_date,'%W')
gets the week number of a column

with me now?


Dang! Yes, I am with you now.

After actually looking at this with new eyes. Couldn't I achieve the same goal doing:
PHP Code:
 SELECT FROM psc_addons
WHERE YEARWEEK
(ada_date) = YEARWEEK(CURRENT_DATE INTERVAL 7 DAY

OR
PHP Code:
 SELECT countada_date 
FROM psc_addons
WHERE YEARWEEK
ada_date ) = YEARWEEKCURRENT_DATE INTERVAL 7 
DAY 


Last edited by gmrstudios : May 8th, 2012 at 09:06 PM.

Reply With Quote
  #8  
Old May 8th, 2012, 09:06 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,443 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 24 m 2 sec
Reputation Power: 4141
Quote:
Originally Posted by gmrstudios
Couldn't I achieve the same goal doing...
bingo

why dontcha test the second one and find out

Reply With Quote
  #9  
Old May 8th, 2012, 09:07 PM
gmrstudios gmrstudios is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 119 gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level)gmrstudios User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 17 h 16 m 15 sec
Reputation Power: 7
Quote:
Originally Posted by r937
bingo

why dontcha test the second one and find out



Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > MSSQL to MYSQL conversion

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap