Link to home
Start Free TrialLog in
Avatar of Nismo83
Nismo83

asked on

Many selection

Can i do a multiple selection of table in a recordset?


strSQL = "SELECT * FROM soccer ORDER BY newsDate DESC"
strSQL2 = "SELECT * FROM basketball ORDER BY newsDate DESC"

Set objRS = Server.CreateObject("ADODB.Recordset")

objRS.CursorLocation = adUseClient
objRS.Open strSQL, strSQL2, objConn
Avatar of sybe
sybe

you could try a UNION, but then the fields of the both tables must be exactly the same.

strSQL = "SELECT * FROM soccer ORDER BY newsDate DESC "
strSQL = strSQL & "UNION "
strSQL = strSQL & "SELECT * FROM basketball ORDER BY newsDate DESC "


You could try

SELECT * FROM soccer, basketball ORDER BY newDate DESC

that will select all fields from both tables.
Avatar of Nismo83

ASKER

SELECT * FROM soccer, basketball ORDER BY newDate DESC
does that means that all tables that i gonna UNION must hav the same field name?

strSQL = "SELECT * FROM soccer ORDER BY newsDate DESC "
strSQL = strSQL & "UNION "
strSQL = strSQL & "SELECT * FROM basketball ORDER BY newsDate DESC "
what if there's any differences?
the UNION requires the fields to have the name names in both tables, but you can rename them in the SELECT statement for each table so the SQL engine knows how to "connect" them

in soccer table
 - player
 - firstname
 - lastname
 - newsDate
in basketball table
 - id
 - first
 - last
 - newsDate

strSQL = "SELECT player AS personID, firstname, lastname, newsDate FROM soccer ORDER BY newsDate DESC "
strSQL = strSQL & "UNION "
strSQL = strSQL & "SELECT id AS personID, first AS firstname, last, newsDate AS lastname FROM basketball ORDER BY newsDate DESC "

so the resulting recordset has three fields personID, firstname, lastname. If the field names differ you must names all fields you need, no *
The corresponding fields must be of the same data type (AFAIK)


CirTap
ASKER CERTIFIED SOLUTION
Avatar of whammy
whammy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the difference between JOINs and UNION is that you'll get additional columns for the joined fields, whereas a UNION used 1 column for matching fields.
Taking my example from above, a JOIN would usually result in a recordset having 6 columns (with whatever names), whereas the UNION would come up with just 3.
So instead of handling individual columns/fields like "socker_player_name", "basketbal_player_name" etc, a UNION would have a single "player_name" column with all people that "play" something (memberlist of a sports-club).
Big difference.

CirTap
Yeah, I guess it all depends upon what data you want to retrieve. Although you could still specify which fields you want in your SQL statement when using a JOIN.
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

<note>
Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
</note>

If the user does not know how to close the question, the options are here:
https://www.experts-exchange.com/help/closing.jsp


Cd&

Hi Cd&,

there are 24 open question by this user... maybe wanna take a look?

Have fun,
CirTap
OHHHHHHHHHH!  some pretty ugly grading too.

I'm not even going to wait for the second pass on this one.  I am posting a request in CS tho have the account suspended.

Cd&