Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - SQL/Access Search for ’Pairs’ of records
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

SQL/Access Search for ’Pairs’ of records

 Post Reply Post Reply Page  12>
Author
schammy View Drop Down
Newbie
Newbie


Joined: 30 July 2003
Location: Israel
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote schammy Quote  Post ReplyReply Direct Link To This Post 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?

Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 30 July 2003 at 3:31pm
I would join the husbands and wives with a JOIN and return them as a unit.
Back to Top
schammy View Drop Down
Newbie
Newbie


Joined: 30 July 2003
Location: Israel
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote schammy Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 30 July 2003 at 3:37pm
It's a SQL command which allows you to combine records which meet your specified criteria.
Back to Top
schammy View Drop Down
Newbie
Newbie


Joined: 30 July 2003
Location: Israel
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote schammy Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
Flamewave View Drop Down
Senior Member
Senior Member
Avatar

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.