Insert + select + as

Discussion in 'MySQL' started by JamesD31, Jan 18, 2012.

  1. #1
    I have been trying to develop a CRON job for the views that I accumulate from a table called 'views'.

    In that table it has the following columns:

    id | type | subtype | date | ip | user

    I have a few tables that get counted on pageview (hence the type/subtype columns). The (ip/user) columns are there for statistical purposes.

    Anyways, what i want to do is the following, I want the cron to run every single day (which I do have setup) but intead of doing a SELECT to get all columns and their counted views, and then perform another INSERT inside a while loop (which is bad SQL programming I know) I want to do it all inside of a single INSERT SELECT statement.

    I have the following so far:

    
    INSERT INTO viewsCondensed (id, type, subtype, date, viewsCount) SELECT views.id, views.type, views.subtype, views.date, COUNT(views.ip) AS viewsCount FROM views GROUP BY views.id, views.type
    
    Code (markup):
    This gives me an error stating their is no field called 'viewsCount'

    So, basically there is no way for me to ever COUNT the items, which is the problem I am having. If I remove that column from the code it runs perfectly fine. I just need a way to COUNT those items and store that new number.

    Anyone have any solutions?
     
    Solved! View solution.
    JamesD31, Jan 18, 2012 IP
  2. #2
    This means you are trying to insert into a field called viewsCount which doesn't exist. Double check the name of the columns in the viewsCondensed table.
     
    jestep, Jan 19, 2012 IP
  3. JamesD31

    JamesD31 Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #3
    >_>..........

    I feel stupid sometimes... but then again that is programming for you haha.
     
    JamesD31, Jan 19, 2012 IP