繁体   English   中英

选择使用双引号字符串时,mysql上出现错误1064 <42000>

[英]Error 1064 <42000> on mysql when selecting using double quoted strings

Zend Framework 2产生的查询:

SELECT "uc".*, "c".* FROM "user_contacts" AS "uc" INNER JOIN "contacts" AS "c" ON "uc"."contact_id" = "c"."contact_id" WHERE "uc"."user_id" = '2' AND "c"."user_id" = '1';

导致此错误(在命令行上运行时):

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.*, "c".* FROM "user_contacts" AS "uc" INNER JOIN "contacts" AS "c" ON "uc"."con' at line 1

此查询(完全相同的查询减去双引号)运行良好:

SELECT uc.*, c.* FROM user_contacts AS uc INNER JOIN contacts AS c ON uc.contact_id = c.contact_id WHERE c.user_id = 2 AND uc.user_id = 1;

+---------+------------+------------+---------+
| user_id | contact_id | contact_id | user_id |
+---------+------------+------------+---------+
|       1 |          7 |          7 |       2 |
+---------+------------+------------+---------+
1 row in set (0.00 sec)

为什么会这样,我该如何解决?

在Ubuntu 12.10上使用AMP堆栈。

表格如下所示:

CREATE TABLE `contacts` (
    `contact_id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) NOT NULL,
    PRIMARY KEY (`contact_id`),
    UNIQUE KEY `contact_id_UNIQUE` (`contact_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

CREATE TABLE `user_contacts` (
    `user_id` int(11) NOT NULL,
    `contact_id` int(11) NOT NULL,
    PRIMARY KEY (`user_id`,`contact_id`),
    KEY `user_contacts_user_id_fkey_idx` (`user_id`),
    KEY `user_contacts_contact_id_idx` (`contact_id`),
    CONSTRAINT `user_contacts_contact_id_fkey` FOREIGN KEY (`contact_id`) REFERENCES    `contacts` (`contact_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
    CONSTRAINT `user_contacts_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Zend db适配器代码:

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=' . $dbName . ';host=' . $host,
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

选择代码:

public function checkIfFriends($currentUserId,$requestedUserId) {
    $currentUserId      = (int) $currentUserId;
    $requestedUserId    = (int) $requestedUserId;

    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from(array('uc' => $this->tableGateway->getTable()))
        ->join(array('c' => 'contacts'), 'uc.contact_id = c.contact_id');

    $where = new Where();
    $where
        ->equalTo('uc.user_id', $currentUserId)
        ->equalTo('c.user_id', $requestedUserId);
    $select->where($where);


    //echo $select->getSqlString();
    $rowSet = $this->tableGateway->selectWith($select);
    $row = $rowSet->current();
    return ($row) ? true: false;  
}

为什么将其作为完全相同的副本关闭? 不是。 我知道问题可能是相同的,但是ZF2正在生成由于引用而无法运行的查询。

在此Zend Framework论坛帖子中找到的解决方案:

是可行的,但实际上不是一个可以接受的解决方案,将不胜感激。

public function checkIfFriends($currentUserId,$requestedUserId) {
    $currentUserId      = (int) $currentUserId;
    $requestedUserId    = (int) $requestedUserId;

    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from(array('uc' => $this->tableGateway->getTable()))
        ->join(array('c' => 'contacts'), 'uc.contact_id = c.contact_id');

    $where = new Where();
    $where
        ->equalTo('uc.user_id', $currentUserId)
        ->equalTo('c.user_id', $requestedUserId);
    $select->where($where);

    $dbAdapter = $this->tableGateway->getAdapter();
    $string = $sql->getSqlStringForSqlObject($select);
    $rowSet = $dbAdapter->query($string, $dbAdapter::QUERY_MODE_EXECUTE);

    $row = $rowSet->current();
    return ($row) ? true: false;  
}

我通过关闭操作系统中的死键设置解决了这个问题。 如何在Linux Mint中执行此操作: http : //forums.linuxmint.com/viewtopic.php?f=55&t=97812

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM