Link to home
Start Free TrialLog in
Avatar of schesht
schesht

asked on

How can I do a SQL query?

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.
ASKER CERTIFIED SOLUTION
Avatar of p_sie
p_sie
Flag of Netherlands image

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
Avatar of schesht
schesht

ASKER

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'
What's your complete query?
Avatar of schesht

ASKER

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.
What do you see when you run the query in Access?
Avatar of schesht

ASKER

in access when i run it i see nothing but field names.
That would mean that no records match the query and what about the query i proposed above
what does that return in access?
Avatar of schesht

ASKER

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
Avatar of schesht

ASKER

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.
Avatar of schesht

ASKER

"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"
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 .....

Avatar of schesht

ASKER

Hi experts,
any hope so far?
Avatar of schesht

ASKER

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.
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!!
Avatar of schesht

ASKER

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.
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
Avatar of Howard Cantrell
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