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 November 20th, 2012, 12:31 PM
anfractuosities anfractuosities is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 anfractuosities User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 55 sec
Reputation Power: 0
Complex MySQL limit Range

I have the following complex query :

Code:
$query = "SELECT c.id as contact_id, c.email, c.name, c.website_name, c.website_url, COUNT(*) as total_emails,
MAX(sent_date) as recent_email, t.clicks, t.views, MAX(t.status) as status, t.post_url, MAX(t.post_date) as post_date
FROM contacts c
 LEFT JOIN emails_sent es ON c.id = es.contact_id 
LEFT JOIN tracking t ON c.id = t.contact_id
GROUP BY c.email
HAVING COUNT(*) = {$_POST['emails_sent']} AND {$condition}
LIMIT 0, {$_POST['contacts']}";


Somewhere in this code I need to limit the results by a date range. The date is stored in the emails_sent DB. Where can this go?

Code:
WHERE sent_date < {$edate} AND sent_date > {$sdate}

Reply With Quote
  #2  
Old November 20th, 2012, 12:33 PM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Something like this - but you should GROUP BY c.id, and I'm not sure if clicks and views is going to give you what you want...
Code:
$query = "
SELECT c.id contact_id
     , c.email
     , c.name
     , c.website_name
     , c.website_url
     , COUNT(*) total_emails
     , MAX(es.sent_date) as recent_email
     , t.clicks
     , t.views
     , MAX(t.status) status
     , t.post_url
     , MAX(t.post_date) post_date
  FROM contacts c 
  LEFT 
  JOIN emails_sent es 
    ON c.id = es.contact_id 
   AND sent_date BETWEEN $sdate AND $edate
  LEFT 
  JOIN tracking t 
    ON t.contact_id = c.id
 GROUP 
    BY c.email
HAVING COUNT(*) = {$_POST['emails_sent']} 
   AND {$condition}
 LIMIT 0, {$_POST['contacts']}";

Last edited by cafelatte : November 20th, 2012 at 12:41 PM.

Reply With Quote
  #3  
Old November 20th, 2012, 12:54 PM
anfractuosities anfractuosities is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 anfractuosities User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 55 sec
Reputation Power: 0
Thanks for the quick response. This does not change the results at all, though.

Reply With Quote
  #4  
Old November 20th, 2012, 01:02 PM
anfractuosities anfractuosities is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 anfractuosities User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 55 sec
Reputation Power: 0
I think that I see what is happening here. Is this correct?

The AND BETWEEN statement, when false, is just falling to append the emails_sent data to the current contact_id.

Reply With Quote
  #5  
Old November 20th, 2012, 01:20 PM
anfractuosities anfractuosities is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 anfractuosities User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 55 sec
Reputation Power: 0
Sorry for the multiple posts....I am trying to rewrite this so the emails_sent table is queried first. The following throws an error unless the WHERE statement OR the JOIN are removed.

Code:
SELECT DISTINCT contact_id
  FROM emails_sent 
   WHERE sent_date 
     BETWEEN 
     $sdate AND $edate

 LIMIT $con
 LEFT
  JOIN contacts c
    ON c.id = contact_id

Reply With Quote
  #6  
Old November 20th, 2012, 01:28 PM
anfractuosities anfractuosities is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 anfractuosities User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 55 sec
Reputation Power: 0
[solved]

Alright! Your first response, that you have since edited, almost nailed it.

Code:
WHERE es.sent_date BETWEEN $sdate AND $edate


before the GROUP BY worked like a charm.

Thanks so much for your help!!!

Reply With Quote
  #7  
Old November 20th, 2012, 02:11 PM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
That renders your LEFT [OUTER] JOIN as an [INNER] JOIN, so you may as well specify it that way to begin with!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Complex MySQL limit Range

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