我在MySQL查询中遇到语法问题。我正在使用Symfony2,我试图通过函数
过滤knownUsersprotected function generateAlias($tag = null)
{
return '___' . $tag . '_' . $this->aliasCounter++;
}
public function filterKnownUsers(\Tuxie\Bundle\TuxieBundle\Entity\User $user, $addMyUser = false)
{
$alias = $this->generateAlias();
$query = "SELECT {$alias}_u2.userId FROM Tuxie\\Bundle\\TuxieBundle\\Entity\\TicketUser {$alias}_u1
JOIN Tuxie\\Bundle\\TuxieBundle\\Entity\\TicketUser {$alias}_u2 WITH {$alias}_u1.ticketId = {$alias}_u2.ticketId
WHERE {$alias}_u1.userId = :{$alias}_user";
if($addMyUser === true) {
$this->where("root.userId IN ($query) OR ({$alias}_u2.userId) ");
}
else {
$this->where("root.userId IN ($query)");
}
$this->setParameter("{$alias}_user", $user);
return $this;
}
因此,如果函数中的第二个参数为true,则过滤器应使用OR语句并给出我唯一的实际用户。不幸的是我收到了语法错误
[2/2] QueryException:[语法错误]第0行,第354行:错误:预期 =,<,< =,<>,>,> =,!=,得到了')'
[1/2] QueryException:SELECT root FROM Tuxie \ Bundle \ TuxieBundle \ Entity \用户root WHERE(root.email不是 NULL AND(root.userId IN(SELECT ____ 0_u2.userId FROM Tuxie \ Bundle \ TuxieBundle \ Entity \ TicketUser ____0_u1加入 Tuxie \ Bundle \ TuxieBundle \ Entity \ TicketUser ____0_u2 WITH ____ 0_u1.ticketId = ____ 0_u2.ticketId WHERE ____ 0_u1.userId =:____ 0_user)OR(____ 0_u2.userId)))AND root.clientId IS NOT NULL
else {
$this->where("root.userId IN ($query)");
}
工作正常。所以问题就在这里
$this->where("root.userId IN ($query) OR ({$alias}_u2.userId) ");
关于正确语法是什么的任何想法?
答案 0 :(得分:1)
({$alias}_u2.userId)
不适合SQL。你需要将价值与某些东西进行比较。
'something'取决于你的表定义。一些可能性:
({$alias}_u2.userId != '') -- for varchar
({$alias}_u2.userId > -1) -- for int
({$alias}_u2.userId NOT NULL) -- for nullable
可用表达式的大量文档:
https://dev.mysql.com/doc/refman/5.5/en/expressions.html