1

会员表:

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 表:

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. advisor_id - 添加社团的用户的唯一 ID
  3. member_id - 指会员表中的“id”。

我希望能够在下拉列表中显示用户尚未选择的成员资格。到目前为止,我有:

$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

4 回答 4

1

FROMNOT EXISTS子查询中缺少。

于 2013-02-11T14:16:11.307 回答
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)

你有一个错误的语法试试这样

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)
于 2013-02-11T12:30:31.003 回答
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 查询问题。

于 2013-02-11T12:31:23.597 回答
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)

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

问候... 777 先生

于 2013-02-11T12:54:58.000 回答