Query like Amazon's "also purchased..."

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Constantine

    Query like Amazon's "also purchased..."

    I have a table "Subscriptions" .

    Subscriptions contains the columns "ChannelID" & "MemberID".

    The "Channels" table contains a column called "Title"

    When someone is viewing a channel, I would like to display "Members
    who subscribe to this channel also subscribe to ..." and then list the
    Titles of 3 or 4 channels ordered by the most popular.

    I'm having trouble coming up with the correct MySQL syntax. Has
    someone here done something like this before?
  • sks

    #2
    Re: Query like Amazon's "also purchased...&qu ot;


    "Tim Constantine" <spam@togosolo. com> wrote in message
    news:bbcd752c.0 410070316.184bb d95@posting.goo gle.com...[color=blue]
    > I have a table "Subscriptions" .
    >
    > Subscriptions contains the columns "ChannelID" & "MemberID".
    >
    > The "Channels" table contains a column called "Title"
    >
    > When someone is viewing a channel, I would like to display "Members
    > who subscribe to this channel also subscribe to ..." and then list the
    > Titles of 3 or 4 channels ordered by the most popular.
    >
    > I'm having trouble coming up with the correct MySQL syntax. Has
    > someone here done something like this before?[/color]

    What do you mean by channel ? If I assume its something like a particular
    magazine, then you want to select all the channels people who bought this
    channel also bought (self join on your table), then order and choose.
    Something like

    select s2.ChannelID, count(*) c from subscriptions s1 join subscriptions s2
    on s1.MemberID=s2. MemberID and s1.ChannelID!=s 2.ChannelID where s1.ChannelID
    = ?? group by s2.ChannelID order by c desc limit 5

    Try that, its untested but should be a start.


    Comment

    • Bill Karwin

      #3
      Re: Query like Amazon's &quot;also purchased...&qu ot;

      Tim Constantine wrote:
      [color=blue]
      > When someone is viewing a channel, I would like to display "Members
      > who subscribe to this channel also subscribe to ..." and then list the
      > Titles of 3 or 4 channels ordered by the most popular.[/color]

      SELECT COUNT(S.MemberI D) AS MemberCount, C.Title
      FROM Subscriptions S INNER JOIN Channels C ON S.ChannelID = C.ChannelID
      INNER JOIN Subscriptions M ON S.MemberID = M.MemberID
      WHERE M.ChannelID = ?
      GROUP BY S.MemberID
      ORDER BY MemberCount DESC
      LIMIT 4

      Substitute the currently viewed channel ID for the '?' parameter in that
      query, and I think that'll work. I tried it out with a set of tables in
      the app I'm working on, where my tables have similar relationships to
      one another as your tables.

      Regards,
      Bill K.

      Comment

      Working...