Jump to content

[SOLVED] MySQL Wizards - Need Help with Query


ryanschefke

Recommended Posts

Hello - I need help with a query. I am programming in PHP and trying to make a MySQL query. I have one table, visitors, and a second table, hits. I am trying to display the results of the visitors table. The columns in the visitors table are organization, city, region, country, dateTime, visitorID, and searchWords. In the output as I display each row, I want to add a column from the hits table that adds all entries in hits with the same vistorID as the particular row I am displaying from the visitors table. I am trying the following query but the COUNT seems to not work and I am not sure if the "hits.visitorID=visitors.visitorID" statement is right. Logically, you can get an idea of what I am trying to do with the erroneous query below. Thanks so much for your help!!!

 

$query = "SELECT visitors.organization, visitors.city, visitors.region, visitors.country, DATE_FORMAT(visitors.dateTime, '%m/%d/%Y %l:%i %p'), visitors.visitorID, visitors.searchWords, COUNT(hits.hitID) FROM visitors, hits WHERE visitors.customerID='$customerID_ck' AND cast(visitors.dateTime as date)='$sGMTMySqlDate' AND hits.visitorID=visitors.visitorID ORDER BY $sort LIMIT $start, $displayToday";

 

Regards,

Ryan

Link to comment
Share on other sites

No doubt you're getting an error that says you can't mix GROUP and no GROUP columns without a GROUP BY clause? Try adding one. Also, you can make your query a bit shorter by using an alias for your table names:

 

$query = "SELECT v.organization, v.city, v.region, v.country, DATE_FORMAT(v.dateTime, '%m/%d/%Y %l:%i %p'), v.visitorID, v.searchWords, COUNT(h.hitID) FROM visitors as v, hits as h WHERE v.customerID='$customerID_ck' AND cast(v.dateTime as date)='$sGMTMySqlDate' AND h.visitorID=v.visitorID GROUP BY h.visitorID ORDER BY $sort LIMIT $start, $displayToday";

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.