473,651 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting/Ordering by prerequisite?

So I have a table that has, among other things, these feilds:

id PRIMARY KEY
name VARCHAR(50)
prerequisite INT NOT NULL

Prerequisite contains a reference to the id of whatever item comes before
it.

How can I generate a query on this that will put them in sequence?
Jun 18 '06 #1
5 1567
AngleWyrm wrote:
Prerequisite contains a reference to the id of whatever item comes before it.
How can I generate a query on this that will put them in sequence?


You haven't described what sequence you want them to be in.

Basically, you're describing tree-structured or heirarchical data, like
a parts-explosion, or threaded forum postings. You can show this
depth-first, breadth-first, or some other sequence.

Regards,
Bill K.
Jun 19 '06 #2
"Bill Karwin" <bi**@karwin.co m> wrote in message
news:e7******** *@enews3.newsgu y.com...
AngleWyrm wrote:
Prerequisite contains a reference to the id of whatever item comes before
it.
How can I generate a query on this that will put them in sequence?


You haven't described what sequence you want them to be in.


An example set of records:

ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2

In the example above, Three comes after Two, because it has a prerequisite
of item ID 2.

This gives a basic feel for the problem. And no, the ID isn't such an neatly
arranged numerical sequence.
Jun 21 '06 #3
AngleWyrm wrote:
ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2

In the example above, Three comes after Two, because it has a prerequisite
of item ID 2.


But what about this case:

ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2
4 |Four |1

What should the sequence be? "One, Two, Three, Four," or "One, Two,
Four, Three"? That's the difference between depth-first and
breadth-first, respectively.

Breadth-first is pretty easy:

SELECT *
FROM this_table
ORDER BY Prerequisite, ID

Depth-first is harder. You probably need to store the relationships
differently. You could implement the heirarchy using the nested-set
data model.
See http://dev.mysql.com/tech-resources/...ical-data.html

Regards,
Bill K.
Jun 22 '06 #4
"Bill Karwin" <bi**@karwin.co m> wrote in message
news:e7******** @enews4.newsguy .com...
AngleWyrm wrote:
ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2

In the example above, Three comes after Two, because it has a
prerequisite of item ID 2.


But what about this case:

ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2
4 |Four |1

What should the sequence be? "One, Two, Three, Four," or "One, Two, Four,
Three"? That's the difference between depth-first and breadth-first,
respectively.

Breadth-first is pretty easy:

SELECT *
FROM this_table
ORDER BY Prerequisite, ID

Depth-first is harder. You probably need to store the relationships
differently. You could implement the heirarchy using the nested-set data
model.
See http://dev.mysql.com/tech-resources/...ical-data.html


There are no cyclical dependencies in the database that I am working with. I
do with it were up to me to alter the structure of the tables to use the
nested-set data model; thank you for the excellent pointer.

Perhaps I can automatically import the data into a private table, and
develop a left and right field for them in the process.
Jun 23 '06 #5
AngleWyrm wrote:
There are no cyclical dependencies in the database that I am working with.


I know what you mean, but for what it's worth, a tree is acyclic by
definition.

Regards,
Bill K.
Jun 23 '06 #6

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

Similar topics

3
3531
by: ams | last post by:
A table contains records of Ice Cream flavors. There is a primary id field (INT) and a varchar for flavor. I need to get the row in the very middle of the table, however I can't assume that the table's primary id field will be entirely sequential across the table because some records may have been deleted. Say there are 10 records in the table and the last primary id is 24. I can't reliably devide 24 by 2 to assume the middle row is...
2
587
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need to provide users with a mechanism to change the order of items that are listed on the page.For example, the _New York Times_ website (http://www.nytimes.com) lists a bunch of top news articles, and normally these are ordered by purely mechanical...
3
2781
by: James Lee | last post by:
I am doing a simple query like col1, col2, date, col4 from table1. All four colums are of type blob. For date column, I store a string like this: Fri Feb 13 11:01:24 2004 I store records as they come in so the oldest record is at the top of the table. When I select and display, I want to display them in reverse order (newest record at the top) but I can't sort on date with strings like that.
4
1602
by: remote89 | last post by:
Hi experts, I have been trying to limit the table rows in the following situation, any suggestions will be appreciated. we have table called tempTb has columns id, c_id, c_name, rating, date columns. id is an identity column. date is a datetime column, the rest are varchar datatype. Here is the table structure with sample data,
6
1827
by: Matik | last post by:
Hello all, I've following problem. Please forgive me not posting script, but I think it won't help anyway. I've a table, which is quite big (over 5 milions records). Now, this table contains one field (varchar), which contains some data in the chain. Now, there is a view on this table, to present the data to user. The
33
3387
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a 4-byte value. Can I use the arithmatic shift operators to hide the endian-ness of the underlying processor when assembling a native 4-byte value like follows: unsigned int integerValue; unsigned char byteArray;
1
1620
by: Bill Nguyen | last post by:
created a CrystalReport10 msi file but can't include it in the prerequisite list using ClickOnce publish. Thanks Bill
1
7183
by: Rich | last post by:
Hello, I am trying to deploy a VB2005 app which contains a ReportViewer control. I have added the Reportviewer control to the prerequisite list by click on Prerequisites in the Project/Properties/Publish/Prerequisites and select which prerequisite to install. I select Microsoft Visual Studio 2005 Report Viewer. I have then also selected "Download prereus.. from vendor site" which did not work and also selected "Download prereq......
5
22755
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset. how do I get a specific subset of rows from the result set if I give a start and end value. For example, retreiving rows 50 to 100 from the resultset.
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.