Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-03-2007, 03:54 PM   PM User | #1
sasha hantz
New to the CF scene

 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
sasha hantz has a little shameless behaviour in the past
Question select * from

i got 2 tables
table 1 called "products" and it got 2 columns : "name" and "loc"
table 2 called "locations" with 2 columns : "id" and "loc"

i need to run search in the products table
i need to find all the records from table "products" that got in their "loc" column
the same locations as in the locations table from row id 7 to id 9

for example if table location -id=7 loc=usa
id=8 loc=uk
id=9 loc=canada
i want to get from the products table all the record where the column "loc"
have usa or uk or canada...

mySQL="SELECT * FROM products WHERE loc=("SELECT * FROM Locations where id>1 and id<10 )
i wrote this select line but it not working...where i got wrong?

10k's
sasha hantz is offline   Reply With Quote
Old 09-03-2007, 04:03 PM   PM User | #2
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
You are not getting this right because your subquery (the "SELECT * FROM Locations where id>1 and id<10 ) will return more than 1 row, causing your main query to error. Also, you have a double quote in the subquery which would cause it to error outright.

Try

Code:
mySQL="SELECT * FROM products WHERE loc IN (SELECT * FROM Locations WHERE id > 1 AND id < 10 )"

OR

mySQL="SELECT * FROM products WHERE loc IN (SELECT * FROM Locations WHERE id BETWEEN 1 AND 10 )"
Lastly, unless absolutely necessary, NEVER USE SELECT *. It does not use indexes (not optimized) because it forces your database to search the ENTIRE table. Always tell the query which fields you want to see.
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.
Daemonspyre is offline   Reply With Quote
Old 09-03-2007, 04:26 PM   PM User | #3
sasha hantz
New to the CF scene

 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
sasha hantz has a little shameless behaviour in the past
you meen to specify a column name instead the *
?
sasha hantz is offline   Reply With Quote
Old 09-03-2007, 05:00 PM   PM User | #4
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
stop cross posting, here or the mysql forum!
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 09-04-2007, 01:11 PM   PM User | #5
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
Yes, I mean use the column (or field) names, not just *.

Say your table1 has 3 columns in it... col1, col2, col3... but you only want to see 2 of those columns.

Instead of

SELECT * FROM table1

which would do a full table scan and not use indexes, and you would have to programatically 'ignore' one field, you would use

SELECT col1, col3 FROM table1

When you tell the database which fields you want to look at, it can use INDEXes that you cannot use with SELECT *.

Hope this helps, and in the future, only post in one forum. It may not be in the right place, but that's what moderators are for - to move posts to the right spots.
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.
Daemonspyre is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:23 PM.


Advertisement
Log in to turn off these ads.