选择From Where Notists查询

时间:2013-02-11 12:25:01

标签: mysql sql

会员资格表:

CREATE TABLE `Consultant_Memberships` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `membership_url` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

Memberships_List table:

CREATE TABLE `Consultant_Memberships_List` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `consultant_id` int(11) DEFAULT NULL,
  `membership_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

在“会员资格”表中,有一个“社团”列表,该成员可以成为其中的一部分。在选择时,然后将其添加到“Memberships_List”中,形式为:

  1. id - 自动增量
  2. consultant_id - 添加社团的用户的唯一ID
  3. membership_id - 指成员资格表中的“id”。
  4. 我希望能够在下拉列表显示用户尚未选择的成员资格。到目前为止我已经:

    $query = $db->query("SELECT `Consultant_Memberships.`id`, `Consultant_Memberships`.`title` `FROM `Consultant_Memberships 
    WHERE NOT EXISTS (SELECT `Consultant_Memberships`.`id`, `Consultant_Memberships`.`title` 
    WHERE `Consultant_Memberships`.`id` = $user_id)");
    

    我目前收到此错误,也不确定这是否是正确的查询:

    PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'WHERE `Consultant_Memberships_List`.`id` = )' at line 1' in /Users/Sites/pages/medical.php:72
    Stack trace:
    #0 /Users/Sites/pages/medical.php(72): PDO->query('SELECT `Consult...')
    #1 /Users/Sites/index.php(18): include('/Users/Site...')
    #2 {main}
      thrown in /Users/Sites/pages/medical.php on line 72
    

4 个答案:

答案 0 :(得分:1)

FROM子查询中缺少

NOT EXISTS

答案 1 :(得分:0)

SELECT `Consultant_Memberships.`id`, `Consultant_Memberships`.`title` `FROM `Consultant_Memberships 
WHERE NOT EXISTS (SELECT `Consultant_Memberships`.`id`, `Consultant_Memberships`.`title` 
WHERE `Consultant_Memberships`.`id` = $user_id)

你有一个错误的语法尝试这样的事情我不是在写exect查询但是在sql小提琴中检查并且那是wrond

SELECT Consultant_Memberships.id, Consultant_Memberships.title FROM Consultant_Memberships 
WHERE NOT EXISTS (SELECT Consultant_Memberships.id, Consultant_Memberships.title from Consultant_Memberships
WHERE Consultant_Memberships.id = 1)

答案 2 :(得分:0)

始终从突出显示的查询部分向左侧观察。你的情况

SELECT `Consultant_Memberships.`id`, `Consultant_Memberships`.`title`
`FROM <--- extra backtick
`Consultant_Memberships <--- unclosed backtick
顺便说一句,不要过度使用反引号。您的大多数字段都不需要

...你的查询完全搞砸了 据我了解你的问题,它必须是

SELECT cm.id, title 
FROM Consultant_Memberships cm LEFT JOIN Consultant_Memberships_List
ON cm.id=membership_id WHERE membership_id IS NULL

请注意,您的问题与PDO无关 这是很清楚的SQL查询问题。

答案 3 :(得分:0)

请试试这个:

SELECT a.id, a.title 
FROM Consultant_Memberships a, Consultant_Memberships_List b
WHERE a.id <> b.consultant_id

您遇到语法错误的原因是因为如果您看到子查询,则会发现您没有在其中指定任何表格。

WHERE NOT EXISTS (SELECT `Consultant_Memberships`.`id`, `Consultant_Memberships`.`title` 
WHERE `Consultant_Memberships`.`id` = $user_id)

如果您需要更多帮助,请告诉我们......

...问候 Mr.777