MySQL Forums
Forum List  »  General

Re: Why does "in ()" give syntax error?
Posted by: Dr Lightman
Date: October 27, 2010 04:12AM

I know I know, I was only looking for a peaceful motivation to this choice, not a fight. I was just curious on why devs decided this way since it wouldn't have do any harm to let zero arguments as parameters from my (poor) point of view.

I've already found the workaround in the scripts to avoid the syntax error, but I generally don't like when I have to add extra logic to the scripts to build up an SQL query :P

What it happens to be done is (PHP example):

$sql = "
	select *
	from customers
	where id in ( " . implode( ',', $allowedIDs ) . "
	order by name asc
";

This works, until $allowedIDs is not empty, eg contains some numerical IDs (34,54,1,...). If it's empty the generated SQL would give syntax error, so the right code would be:

if( empty( $allowedIDs ) ) {
	$sql = "
	select *
	from customers
	where false
";
} else {
	$sql = ... // the one of the code above //
}

I know this example may be dumb, because in this case I could have returned an empty result set as soon as I know the $allowedIDs is empty, but think of this $sql as a more complex one with joins and many other conditions.

Maybe using () would let MySQL interpret as if it was one parameter of NULL value or empty string, so to avoid misunderstandings they imposed to be "something" between brackets. It's just a weak idea, I don't know if it has to do with the real motivation.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.