Jump to content

[SOLVED] joining two tables at interesting points


ultrus

Recommended Posts

Hello,

Getting a query made that combines the number of visits and purchases on a website is giving me a headache. I'm having difficulties joining a couple tables by the same on-the-fly created columns "ym" (year month). How would you do this? Here is what I have so far:

 

Table: visits

id (auto incremented)

ip (user's ip for use in separate unique visit query unrelated to this post)

time (timestamp e.g. 2008-05-11 22:11:13)

 

Table: purchases

id (auto incremented)

buyer_id (provided by payment processor, unrelated to this post)

time (same format as visits table)

 

To get number of visits per month, this query brings great success:

SELECT DATE_FORMAT(time, '%b %Y') ym, DATE_FORMAT(time, '%c') month, DATE_FORMAT(time, '%Y') year, COUNT(*) as hits FROM visits GROUP BY ym

 

I could probably get the number of purchases per month like this:

SELECT DATE_FORMAT(time, '%b %Y') ym, DATE_FORMAT(time, '%c') month, DATE_FORMAT(time, '%Y') year, COUNT(*) as hits FROM purchases GROUP BY ym

 

How would I join the two queries into one?  ??? This is probably easier than I'm making it out to be. It's probably time for sleep.  >:(

 

Thank you much in advance. :)

Link to comment
Share on other sites

Well, now that I think about it, the question now is: Why join?

 

I found another way around my problem by doing a second, separate query. It works well in the formatting of my page. Thanks for the feedback. I think It's time to sleep now.  8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.