Solved

How can I do a SQL query?

Posted on 2003-12-09
18
231 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now