DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Databases > MySQL

Discuss "Using a field value in an IN(...) clause" in the MySQL forum.

MySQL - MySQL is a powerful open source database system most commonly used with PHP applications. Discuss MySQL administration and other MySQL related topics here.


Reply
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 04-07-2008, 09:18 AM
richyrich's Avatar
Moderator

 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 173
Thanks: 12
Thanked 3 Times in 3 Posts
Rep Power: 1
richyrich is on a distinguished road
Default Using a field value in an IN(...) clause

Anyone know why this works:-
Code:
select value1,value2,value3 FROM tbl1 WHERE tbl1.value4='a value' AND tb1.value5 IN(2,3,5)
But this doesn't:-
Code:
select value1,value2,value3 FROM tbl1,tbl2 WHERE tbl1.value4='a value' AND tb1.value5 IN(tbl2.value1)
Assuming tbl2.value1 = "2,3,5"

For some reason it only seems to take into account the first value in the table field, but if you type the figures manually, it uses them all.

Any ideas?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-07-2008, 10:06 AM
mehere's Avatar
Mistress of Sarcasm

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 75
Thanks: 5
Thanked 11 Times in 9 Posts
Rep Power: 1
mehere is on a distinguished road
Default

i think it has something to do with not going through the whole table with the way the query is. try this:
Code:
SELECT value1,value2,value3 
FROM tbl1 
WHERE tbl1.value4='a value' 
AND tb1.value5 IN
	(SELECT value1 FROM tbl2)
although i think this will work with mySQL 5, it may not work with mySQL 4. not sure. I haven't used mySQL all that much.
__________________
Quote of the Month:
Strife: As long as we have each other, we'll never run out of problems.

Questions to Ponder:
I went to a bookstore and asked the saleswoman where the self-help section was and she said if she told me, it would defeat the purpose.

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
Reply With Quote
  #3 (permalink)  
Old 04-07-2008, 10:52 AM
richyrich's Avatar
Moderator

 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 173
Thanks: 12
Thanked 3 Times in 3 Posts
Rep Power: 1
richyrich is on a distinguished road
Default

Thanks for the reply mehere.

I tried that aswell, but get the same (wrong) result. I also tried adding the values as a parameter with the same result.

It only seems to accept it, if it's physically in the SQL statement. Something like:-
Code:
strsql = "select value1,value2,value3 FROM tbl1,tbl2 WHERE tbl1.value4='a value' AND tb1.value5 IN(" & my_variable  &")"
This returns the correct results for me. I guess it has something to do with the way it's interpreting the values before it puts them in the query.

It does actually give a result, which is the strange thing, but only based on the first value in the array.

Ah well, guess I'll have to stick with the syntax above.

It's not a major issue because the values I'm inserting aren't selected by the user or anything so it should minimise any problems.
Reply With Quote
  #4 (permalink)  
Old 04-07-2008, 10:54 AM
mehere's Avatar
Mistress of Sarcasm

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 75
Thanks: 5
Thanked 11 Times in 9 Posts
Rep Power: 1
mehere is on a distinguished road
Default

hmmm ... strange. i do know that this works in MSSQL, but my mySQL is a bit rusty ...
Reply With Quote
  #5 (permalink)  
Old 04-07-2008, 11:22 AM
richyrich's Avatar
Moderator

 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 173
Thanks: 12
Thanked 3 Times in 3 Posts
Rep Power: 1
richyrich is on a distinguished road
Default

Never mind...Guess I'll just have to stick with it like this.

I just assumed there would be a way to do this, but I guess not...

Thanks mehere...
Reply With Quote
  #6 (permalink)  
Old 04-13-2008, 11:56 AM
don94403's Avatar
New Member
 
Join Date: Mar 2008
Location: San Mateo, CA, USA
Posts: 22
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 1
don94403 is on a distinguished road
Default

Quote:
Originally Posted by richyrich View Post
Anyone know why this works:-
Code:
select value1,value2,value3 FROM tbl1 WHERE tbl1.value4='a value' AND tb1.value5 IN(2,3,5)
But this doesn't:-
Code:
select value1,value2,value3 FROM tbl1,tbl2 WHERE tbl1.value4='a value' AND tb1.value5 IN(tbl2.value1)
Assuming tbl2.value1 = "2,3,5"

For some reason it only seems to take into account the first value in the table field, but if you type the figures manually, it uses them all.

Any ideas?
I think you may have to enclose the value in literal quotes, like:
Code:
... AND tbl1.value5 IN("'"+tbl2.value1+"'")
but I wouldn't bet money on it.
Reply With Quote
Reply

  DeveloperBarn Forums > Databases > MySQL

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/mysql/108-using-field-value-clause.html
Posted By For Type Date
DeveloperBarn Forums - ASP Help, ASP.Net Help, PHP Help, SQL Help, Tutorials, Windows Help This thread Refback 04-26-2008 09:36 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.


All times are GMT -4. The time now is 01:06 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
©2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45