Sql Join Statement

I want to combine the two select statements and use the output to insert into another table.
The following are all three individually.


select tblStudent.dIDNumber,tblStudent.lLostCardIdentifier from tblStudent where tblStudent.lMealPlan between 200 and 297 or tblStudent.dIDNumber in (16734300,43384500)

select tblEvents.achEventName,tblEvents.achRemoveDate,bGeneralAccess,tblEvents.lEventNumber from tblEvents where tblEvents.lEventNumber = 601

insert into tblStudentEvents (dIDNumber,lLostCardIdentifier,lEventNumber,lTickets,achEventName,achRemoveDate,bGeneralAccess)
values (tblstudent.dIDNumber,tblstudent.lLostCardIdentifier,tblevents.lEventNumber,1,tblevents.achEventName,tblevents.achEventName,tblevents.bGeneralAccess)
MillkindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oleggoldCommented:
insert into tblStudentEvents
select tblStudent.dIDNumber,tblStudent.lLostCardIdentifier from tblStudent where tblStudent.lMealPlan between 200 and 297 or tblStudent.dIDNumber in (16734300,43384500)
0
oleggoldCommented:
You need to use insert as select combination
0
oleggoldCommented:
insert as select combination:
insert into tblStudentEvents 
select tblStudent.dIDNumber,tblStudent.lLostCardIdentifier from tblStudent where tblStudent.lMealPlan between 200 and 297 or tblStudent.dIDNumber in (16734300,43384500) join
(select tblEvents.achEventName,tblEvents.achRemoveDate,bGeneralAccess,tblEvents.lEventNumber from tblEvents where tblEvents.lEventNumber = 601) on ...

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

MillkindAuthor Commented:
That the problem there is no on, there is not unique id between them.
0
oleggoldCommented:
as per abov e You need something to join on between students and events , please paste the structure of both tables if You need help identifying what attribute is used to join those 2 tables.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I guess s that both SELECT return one row.
You can do
Insert into ...
Select...
From table1 , table2
Where...
0
MillkindAuthor Commented:
No one will return multiple rows the other will return only one.  I think im starting to see the problems here.  For each return of ID and LostCardID I want it to use the same line of
tblEvents.achEventName,tblEvents.achRemoveDate,bGeneralAccess,tblEvents.lEventNumber from tblEvents where tblEvents.lEventNumber = 601) for each one. it inserts.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
then my comment will still apply.
insert into tblStudentEvents (dIDNumber,lLostCardIdentifier,lEventNumber,lTickets,achEventName,achRemoveDate,bGeneralAccess)
SELECT s.dIDNumber, s.lLostCardIdentifier, e.lEventNumber,1, e.achEventName, e.achEventName, e.bGeneralAccess
 FROM tblEvents e
   , tblStudent s 
WHERE e.lEventNumber = 601
  AND ( s.lMealPlan between 200 and 297 or s.dIDNumber in (16734300,43384500) )

Open in new window


instead of the "," you could as well write CROSS JOIN, it would have the same effect.
note the use of the table aliases I put in the query, explained here:
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MillkindAuthor Commented:
Perfect. And the explanation link was superb.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.