Jump to content
MakeWebGames

MySQL query with EXTREMELY long Where clause


Recommended Posts

Hey all,

Had a question regarding the where clause for mysql.

How long can it be?

Say I wanted to do something like:

SELECT * FROM tablename WHERE id=1 or id=7 or id=19... and on and on for, say, 300 conditions (that are essentially random, and cannot be turned into an algorithm)

So is there a limit? I imagine there has got to be.

Also - can you think of a more efficient way to do this?

Link to comment
Share on other sites

There is a limit, but not due to the size of the condition, it would be something like the limit of the string size of the query, but honestly I never reached it.

Now for your query instead of having id=1 or id=2 etc use

[mysql]SELECT * FROM tablename WHERE id in (1,7,9,10)[/mysql]

It will be faster, shorter to write, and more easy to read. Also if you produce this list from php you can do something like:

 

$search=array();
for($i=0;$i < 10;$i++)
   $search[]=mt_rand(1,1000);
$sql="SELECT * FROM tablename WHERE id in (".implode(",",$search).")";
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...