472,799 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

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.

Jul 23 '05 #1
4 2080
Diamondback wrote:
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.


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|year
---------|--------|-----------|----
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{"$var_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(fatalsToBrowser);
$db = DBI->connect("DBI:mysql:NAME:HOST","USR", "PW");
$query = $db->prepare("SELECT
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{"$var_old_id"} = $var_name;
}
$query->finish;
$db->disconnect;
print "w.name|w.widget_id|v.new_id|v.old_id|\n";
$db = DBI->connect("DBI:mysql:NAME:HOST","USR","PW");
$query = $db->prepare("SELECT
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{$array[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

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

Jul 23 '05 #3
On 26/05/2005, Diamondback wrote:
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.


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
Jul 23 '05 #4
That did it exactly. Thank you.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
A very strange bug. www.monkeyclaus.org is run by a cms I'm developing. One of types of users we allow is "justTestingTheSite", a type of user I developed to give demo's to prospective clients. The...
14
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
6
by: Steven D.Arnold | last post by:
I have a query which does not use column indexes that it should use. I have discovered some interesting behaviors of Postgres which may indicate a bug in the database's query planning. Take a...
2
by: Pete | last post by:
Before I get started with the question, does anyone have a (single) good book recommendation for database design? Not an Access-specific book, but something geared toward helping me figure out...
4
by: dp | last post by:
After looking and looking, it appears that Access ADPs graphic query designer won't display: update customer set = . from customer, where customer. = .; It comes up with the "Query...
3
by: brian | last post by:
I have a problem that probably has a simple solution but I don't have much SQL experience... My company's database is pretty crummy, based on Access97 .MDB files with a VB front-end. My...
2
by: John Hoge | last post by:
I'm looking for that elusive .net time saving magic. I have an application that presents a list of items which the user can select from. A CheckBoxList works great for that, but I want to query...
3
by: Phil Stanton | last post by:
I have a number of queries which use code for the output of 1 or more fields. For example Address:GetAddress(AddressID, True, 60) Address ID Points to an Address in a table - Address Line1, Line...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.