| Author |
Topic Search Topic Options
|
schammy
Newbie
Joined: 30 July 2003
Location: Israel
Status: Offline
Points: 4
|
Post Options
Thanks(0)
Quote Reply
Topic: SQL/Access Search for Pairs of records Posted: 30 July 2003 at 2:58pm |
|
(I asked this question on a different Forum, but don't have a suitable answer <or else nobody understands what I'm talking about!>).
I have an Access database in which each record has personal details such as 'year', 'no.', 'first name' and 'surname'. Now, each person in the database has a matching partner (for simplicity, let's call the records 'husband' and 'wife'). Each pair is defined by a unique common record ('year', 'no').
I want to search a surname or firstname and then return that record PLUS the partner record whether or not that partner also satisfies the search criterion.
For example:
I will search by a name (eg., 'JONES') and the resulting table should look like this:
Surname First_Name Year No. JONES Tom 1970 23 SMITH Maggie 1970 23 BUSH George 1956 65 JONES Marion 1956 65
In other words, a search for 'JONES' returns 2 record pairs comprising 4 records.
My search query at the moment is as follows:
strSQL = "SELECT year, no, surname, first_name " _ & "FROM marriages " _ & "WHERE surname LIKE '%" & Replace(strSearch, "'", "''") & "%' " _ & "OR first_name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _ & "ORDER BY surname;"
And the records are returned as follows:
<% Do While Not rstSearch.EOF And rstSearch.AbsolutePage = iPageCurrent %> <tr> <td><%= rstSearch.Fields("year").Value %></td> <td><%= rstSearch.Fields("no").Value %></td> <td><%= rstSearch.Fields("surname").Value %></td> <td><%= rstSearch.Fields("first_name").Value %></td> </tr>
<% rstSearch.MoveNext Loop %>
In the "JONES" example above, this code will only return the 1st and 4th records.
HOW DO I RETURN "PAIRS" OF RECORDS (HUSBAND AND WIFE) FROM THE DATABASE?
|
 |
ljamal
Mod Builder Group
Joined: 16 April 2003
Status: Offline
Points: 888
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:31pm |
|
I would join the husbands and wives with a JOIN and return them as a unit.
|
|
|
 |
schammy
Newbie
Joined: 30 July 2003
Location: Israel
Status: Offline
Points: 4
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:33pm |
|
Thanks Jamal, but I am not a programmer. What do you mean by JOIN? Is this an SQL command or an ACCESS function?
|
 |
ljamal
Mod Builder Group
Joined: 16 April 2003
Status: Offline
Points: 888
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:37pm |
|
It's a SQL command which allows you to combine records which meet your specified criteria.
|
|
|
 |
schammy
Newbie
Joined: 30 July 2003
Location: Israel
Status: Offline
Points: 4
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:39pm |
|
I thought JOIN is used when one has 2 DIFFERENT tables. All my records ar in 1 table.
|
 |
ljamal
Mod Builder Group
Joined: 16 April 2003
Status: Offline
Points: 888
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:44pm |
|
You can join a table with itself as long as you declare a different name for one instance of the table
For instance you can do:
select Table.Pkey, Tble.PKEY from Table, Table Tble where tble.Pkey = Table.PKey+3
|
|
|
 |
Flamewave
Senior Member
Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:47pm |
This page may help you to understand the JOIN command better:
http://www.w3schools.com/sql/sql_join.asp
|
|
- Flamewave
They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 30 July 2003 at 3:52pm |
To do just a join on the No. Field, well not really a join more of a Subquery you could do something like: select * from tblTest where [No.] IN (Select [No.] from tblTest where Surname = 'Jones')
Now you just have to adjust the subquery to your LIKE query for both field but you should get the jist of it.
|
|
|
 |