Solved

How can I do a SQL query?

Posted on 2003-12-09
18
236 Views
Last Modified: 2011-09-20
Hi Experts,
I have problem retrieving data out of my database. Using access, I work on a Visual basic school project assignment. I have 3 tables. stock_no is the primary key in my inventory table. a_stock_no is a field in another table which matches primary key. and i_stock_no is another field in a different table which also matches primary key. I'm looking for a SQL query to display one record at a time. I tried this: "SELECT * FROM TABLE1,TABLE2,TABLE3 WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO AND TABLE1.STOCK_NO = TABLE3.I_STOCK_NO" I got an error saying: EOF or BOF empty or something like that. then I tried to change AND to OR. it worked but not correctly. it displayed each record twice. first matched with A_STOCK_NO then matched with I_STOCK_NO. hope I'm clear. thanks for any help.
0
Comment
Question by:schesht
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
18 Comments
 
LVL 9

Accepted Solution

by:
p_sie earned 50 total points
ID: 9906072
How about

SELECT DISTINCT TABLE1.* FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO

Distinct means that only distinct values are displayed
When using * with multiple tables always mention the tablename in front!!

Good luck
0
 

Author Comment

by:schesht
ID: 9906248
Hi p_sie,
I'm getting a syntax error saying: missing operator in query expression 'TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO'
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9906479
What's your complete query?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:schesht
ID: 9906570
i've tried this it kinda worked for me the way i wanted but there's an issue. evey time i want to go to the next record i have to click next button twice. the query i tried is as follows:
"SELECT DISTINCT * FROM TABLE1,TABLE2,TABLE3 WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO OR TABLE1.STOCK_NO = TABLE3.I_STOCK_NO OR TABLE1.PRODUCT = 'For Sale' ORDER BY TABLE1.STOCK_NO"
i dont know why i have to click two times.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9906761
What do you see when you run the query in Access?
0
 

Author Comment

by:schesht
ID: 9907062
in access when i run it i see nothing but field names.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9907111
That would mean that no records match the query and what about the query i proposed above
what does that return in access?
0
 

Author Comment

by:schesht
ID: 9907415
i think i said I'm getting a syntax error with your query-  saying: missing operator in query expression 'TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO' . and the full query is like:
SELECT DISTINCT TABLE1.* FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.STOCK_NO = TABLE2.A_STOCK_NO INNER JOIN TABLE3 ON TABLE1.STOCK_NO = TABLE3.I_STOCK_NO
0
 

Author Comment

by:schesht
ID: 9908167
Ok,
I ran the following query in access the outcome displayed as each record i mean same record twice for the matching criteria. is there any way i can fix it.
0
 

Author Comment

by:schesht
ID: 9908173
"SELECT DISTINCT * FROM TABLE1,TABLE2,TABLE3 WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO OR TABLE1.STOCK_NO = TABLE3.I_STOCK_NO OR TABLE1.PRODUCT = 'For Sale' ORDER BY TABLE1.STOCK_NO"
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9910512
There has got to be a field in the double results that is not the same as you have DISTINCT in your query, this always filters out the double
results.
Try to explicitely type the fields you want to query, like:

SELECT DISTINCT TABLE1.Stock_no, TABLE2.A_STOCK_NO FROM .....

0
 

Author Comment

by:schesht
ID: 9910528
Hi experts,
any hope so far?
0
 

Author Comment

by:schesht
ID: 9910556
the redundancy actually depends on the number of the records i have in my database in all three tables. what i mean is that i entered 4 records the out come was four times now. in other words, if i have 100 records in my tables 2 and 100 records in my table 3 it displays 200 records. the number of records in table2 plus the number of records in table3. I hope i'm clear by all this. cause i'm confused myself.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9910612
That's because you have the following:

WHERE TABLE1.STOCK_NO = TABLE2.A_STOCK_NO OR TABLE1.STOCK_NO = TABLE3.I_STOCK_NO

A record matches your criteria if it's Table1.Stock_no equals table2.a_stock_no OR table3.i_stock_no
SO the record is displayed twice because you use SELECT * FROM , this way all posibilities are displyed
Like I said before, try using INNER JOIN's OR maybe OUTER JOIN's , Try making the query you want in access and then
right click in the area and select SQL , you will see the SQL-query as it is correct!!
0
 

Author Comment

by:schesht
ID: 9914311
Hello p_sie,
do you think you can check my access file if by any way I can send it to you. that way you may come up to alternative way to do things around. I would really appreciate that.
0
 
LVL 9

Expert Comment

by:p_sie
ID: 9918747
I will have a look at it if you put it on the internet somewhere, just let me know where to get it.

Greetings, p_sie
0
 
LVL 27

Expert Comment

by:planocz
ID: 10234871
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
No response from schesht from 12/11/2003 comment
Award points to p_sie is recommend.
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

planocz
EE Cleanup Volunteer
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question