Re: Finding Records
From: Stephen Howe (stephenPOINThoweATtns-globalPOINTcom)
Date: 01/25/05
- Next message: DS: "ADO Recordcount = -1"
- Previous message: Frank Hickman [MVP]: "Re: Finding Records"
- In reply to: Geoff Jones: "Q: Finding Records"
- Next in thread: Geoff Jones: "Re: Finding Records"
- Reply: Geoff Jones: "Re: Finding Records"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 25 Jan 2005 16:03:52 -0000
> Can anybody give me some advice on the following?
>
> Suppose I have two tables: A and B. There is a one to many relationship
> between A and B. I would like to find all the child rows in B for a given
> row in A. Then, from these returned rows, I would like to find all the
rows
> which satisfy a given criteria.
>
> With me so far? :)
Yes. Use SQL. You don't want to ever do in code what can be done in SQL.
The SQL engine for most databases is more powerful than what hand-coding
with loops could achieve. So use it :-)
If you find yourself iterating through Recordsets, comparing doing finds
etc, this will normally always be a poorer solution than 1 SQL command that
achieves the same results. Same for INSERTs, DELETEs, UPDATEs even if
complex. So you want to learn how to express yourself in SQL. It is very
powerful.
For this problem you want an (INNER) JOIN and a WHERE clause
You don't say what you want to do with the rows you get back. I assume you
want to see them, so SELECT
SELECT A.field1, A.field2, A.fieldN, B.field1, B.field2
FROM A JOIN B ON A.fieldincommon1=B.fieldincommon2
[WHERE yourcriteria]
[ORDER BY 1,2,3]
should do it.
(i) In the SELECT clause put whatever fields you want to see returned
(ii) In the optional WHERE clause specify your criteria for restriction.
Without a WHERE clause all matching rows are returned
(iii) In the optional ORDER clause 1,2,3 means order by the columns
specified in the SELECT clause. You can do
ORDER BY A.field1, A.field2, A.fieldN
instead. If you don't care about ORDER, records may come back in any old
order.
In ADO, if this was a Recordset Open() or Command Execute, you will get back
a recordset consisting of the fields you select and only records that match
the WHERE clause.
For each entry in A you will get back as many B entries that match and A
fields will be duplicated for that A entry but the B fields in the SELECT
will be what matches the A entry.
Stephen Howe
- Next message: DS: "ADO Recordcount = -1"
- Previous message: Frank Hickman [MVP]: "Re: Finding Records"
- In reply to: Geoff Jones: "Q: Finding Records"
- Next in thread: Geoff Jones: "Re: Finding Records"
- Reply: Geoff Jones: "Re: Finding Records"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|