Query looking back

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

    #1

    Query looking back

    I have two tables, WIDGETS and VERSIONS. The WIDGETS table has
    descriptive information about the widgets while the VERSIONS table
    contains IDs relating to different iterations of those widgets over the
    years. The idea is that from any widget in the database you can look
    forward and backward to see different versions of the widget through
    the years. Here are the tables:

    WIDGETS
    widget_id
    name
    description
    year

    VERSIONS
    v_id
    new_id
    old_id


    Now my query gets me somewhere in the vicinity, but not where I want to
    be.

    select w.widget, w.widget_id, v.new_id, v.old_id
    from widgets w, versions v
    where w.widget_id = 712
    and w.widget_id = v.new_id
    order by w.widget;

    .... and I get ...

    WIDGET WIDGET_ID NEW_ID OLD_ID

    Widget 1 712 712 535
    Widget 1 712 712 287
    Widget 1 712 712 1
    Widget 1 712 712 205
    Widget 1 712 712 458

    What I would like is a list that shows the widget names related to
    OLD_ID, rather than NEW_ID. But I'm not sure how to get there. Help
    is appreciated.

  • Bart Van der Donck

    #2
    Re: Query looking back

    Diamondback wrote:
    [color=blue]
    > I have two tables, WIDGETS and VERSIONS. The WIDGETS table has
    > descriptive information about the widgets while the VERSIONS table
    > contains IDs relating to different iterations of those widgets over the
    > years. The idea is that from any widget in the database you can look
    > forward and backward to see different versions of the widget through
    > the years.
    >
    > Now my query gets me somewhere in the vicinity, but not where I want to
    > be.
    >
    > select w.widget, w.widget_id, v.new_id, v.old_id
    > from widgets w, versions v
    > where w.widget_id = 712
    > and w.widget_id = v.new_id
    > order by w.widget;
    >
    > ... and I get ...
    >
    > WIDGET WIDGET_ID NEW_ID OLD_ID
    >
    > Widget 1 712 712 535
    > Widget 1 712 712 287
    > Widget 1 712 712 1
    > Widget 1 712 712 205
    > Widget 1 712 712 458
    >
    > What I would like is a list that shows the widget names related to
    > OLD_ID, rather than NEW_ID. But I'm not sure how to get there. Help
    > is appreciated.[/color]

    Maybe there is a plain sql solution out there, but it will definitely
    not be an easy one. Maybe it's possible with a join/subquery
    combination or stuff in that direction.

    For code transparency and general ease of use I'ld suggest an API
    approach. You could execute 2 separate queries where the first one ties
    each [v]![old_id] to its corresponding [w]![name]. The second query
    could then use these stored values from the first query.

    I set up the following test environment:

    SELECT * FROM w;

    ---------|--------|-----------|----
    widget_id|name |description|ye ar
    ---------|--------|-----------|----
    40|Widget 0| |
    400|Widget 1| |
    500|Widget 2| |
    600|Widget 3| |
    700|Widget 4| |
    712|Widget 5| |
    ---------|--------|-----------|----

    SELECT * FROM v;

    ----|------|------
    v_id|new_id|old _id
    ----|------|------
    1|712 |400
    2|712 |500
    3|712 |600
    4|712 |700
    5|713 |50
    6|713 |80
    7|714 |40
    ----|------|------

    Now the API could first perform the following query:

    SELECT v.old_id, w.name
    FROM v
    LEFT JOIN w
    ON v.old_id = w.widget_id

    And then tie those results to, let's say, a hash:

    $widgetname{"$v ar_old_id"} = $var_name;

    (supposed that $var_old_id is the first field and $var_name the second
    one from your query result set)

    Then, doing a query like:

    SELECT v.old_ID AS name, w.widget_id, v.new_id, v.old_id
    FROM w, v
    WHERE w.widget_id =712
    AND w.widget_id = v.new_id
    ORDER BY w.name

    will give you:

    ----|---------|------|------
    name|widget_id| new_id|old_id
    ----|---------|------|------
    400 |712 |712 |400
    500 |712 |712 |500
    600 |712 |712 |600
    700 |712 |712 |700
    ----|---------|------|------

    Finally, before showing results to screen, replace the first column by
    its descriptive name that was stored in your hash.

    I realise this all sounds rather cryptic. Here is a small perl program
    (tested):

    #!/usr/bin/perl
    use DBI;
    # uncomment the following 2 lines if CGI
    # print "Content-Type: text/html\n\n<html>\ n<pre>\n";
    # use CGI::Carp qw(fatalsToBrow ser);
    $db = DBI->connect("DBI:m ysql:NAME:HOST" ,"USR", "PW");
    $query = $db->prepare("SELEC T
    v.old_id, w.name
    FROM v
    LEFT JOIN w
    ON v.old_id = w.widget_id
    ");
    $query->execute;
    $numrows = $query->rows;
    while (@array = $query->fetchrow_array )
    {
    my ($var_old_id, $var_name)= @array;
    $widgetname{"$v ar_old_id"} = $var_name;
    }
    $query->finish;
    $db->disconnect;
    print "w.name|w.widge t_id|v.new_id|v .old_id|\n";
    $db = DBI->connect("DBI:m ysql:NAME:HOST" ,"USR","PW") ;
    $query = $db->prepare("SELEC T
    v.old_ID, w.widget_id,
    v.new_id, v.old_id
    FROM w, v
    WHERE w.widget_id =712
    AND w.widget_id = v.new_id
    ORDER BY v.old_ID");
    $query->execute;
    $numrows = $query->rows;
    while (@array = $query->fetchrow_array )
    {
    print "$widgetname{$a rray[0]}|";
    print $array[1]."|";
    print $array[2]."|";
    print $array[3]."|\n";
    }
    $query->finish;
    $db->disconnect;
    # uncomment this line if CGI
    print "</pre>\n</html>";

    Output result (when ran as non-CGI):

    w.name|w.widget _id|v.new_id|v. old_id|
    Widget 1|712|712|400|
    Widget 2|712|712|500|
    Widget 3|712|712|600|
    Widget 4|712|712|700|

    The same script can be made in your favourite api language if you're
    not a perl man.

    Hope this helps.

    --
    Bart

    Comment

    • Diamondback

      #3
      Re: Query looking back

      You're right, it does sound cryptic. But I'm giving it a try ... I'll
      let you know how it turns out.

      Comment

      • Felix Geerinckx

        #4
        Re: Query looking back

        On 26/05/2005, Diamondback wrote:
        [color=blue]
        > select w.widget, w.widget_id, v.new_id, v.old_id
        > from widgets w, versions v
        > where w.widget_id = 712
        > and w.widget_id = v.new_id
        > order by w.widget;
        > ...
        > What I would like is a list that shows the widget names related to
        > OLD_ID, rather than NEW_ID. But I'm not sure how to get there. Help
        > is appreciated.[/color]

        Something like this?

        select w.name, ow.name as oldname, w.widget_id, v.new_id, v.old_id
        from widgets w, versions v, widgets ow
        where w.widget_id = 712
        and w.widget_id = v.new_id
        and ow.widget_id = v.old_id
        order by w.name;

        --
        felix

        Comment

        • Diamondback

          #5
          Re: Query looking back

          That did it exactly. Thank you.

          Comment

          Working...