in reply to newbie table structure

Instead of having ten tables like that, which will be ugly for your program once you want to add another search engine, have two tables, one for the "visit", and one for the "search_engine", like that:

-- SQL code: CREATE TABLE IF NOT EXISTS visit ( id int auto_increment not null, engine_id int, ip int, date date, primary key (id) ); CREATE TABLE IF NOT EXISTS search_engine ( id int auto_increment not null, useragent int, name char(256), homepage char(256) );

You then add, for every visit, a new row into the visit table, and put in the number of the search engine from the search_engine table. Having all the data in one place makes then selecting the last 10 engines very easy:

select distinct search_engine.id from search_engine inner join visit on search_engine.id = visit.engine_id order by visit.date desc limit 10;

I'm not exactly sure if you want to show the last 10 visiting search engines, or if you want to show the search engines, if any, that made the ten last hits. Getting the search engine(s) for the ten last hits is harder:

select distinct search_engine.id from search_engine inner join (select visit.id from visit order by visit.date desc limi +t 10);

Replies are listed 'Best First'.
Re^2: newbie table structure
by coldfingertips (Pilgrim) on Jan 29, 2005 at 19:07 UTC
    Thanks.

    I think I'll try your code but I do have a few questions.

    What is "engine_id", "name" and "homepage"? I don't know what information you're expecting to be in these fields. And why is useragent and integer in SEARCH_ENGINE when it's not an integer?

    Thanks.

      I added "name" and "homepage" as ideas for additional information you might want to store for each search engine - I should have said that. Having a central location to store that data allows you to easily update the information without having several places where you need to change it.

      The engine_id in the table visits points to the search engine that visited you. That useragent is an int in the search engine table is an error - it should be char(250) or something like that.