MySQL 查询有问题。仅返回结果 GROUP_CONCAT 数组中第一项的结果。即结果数组为 [1,3,4],仅返回用户 ID 1 的问卷。
$query_search = "SELECT questionnaires_index.id, questionnaires_index.ea_num, questionnaires_index.address, questionnaires_index.status, questionnaires_index.json_stored, users.username FROM questionnaires_index INNER JOIN users ON users.id = questionnaires_index.interviewer_id WHERE questionnaires_index.interviewer_id IN (SELECT GROUP_CONCAT(id) FROM users WHERE supervisor = (SELECT id FROM users WHERE username = '".$username."'))";
我是否错误地使用了 GROUP_CONCAT?有没有更好的方法来做到这一点?
编辑 1 这是此查询中使用的 SQL 表
CREATE TABLE IF NOT EXISTS `questionnaires_index` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ea_num` int(10) unsigned NOT NULL,
`address` varchar(100) NOT NULL,
`interviewer_id` int(10) unsigned NOT NULL,
`status` varchar(30) NOT NULL DEFAULT 'Available',
`json_stored` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
--
-- Dumping data for table `questionnaires_index`
--
INSERT INTO `questionnaires_index` (`id`, `ea_num`, `address`, `interviewer_id`, `status`, `json_stored`) VALUES
(1, 101, '29 De Havilland Crescent Pro Park, Building 1 Persequor Technopark Pretoria 0020', 1, 'Non Contact', 1),
(2, 102, '5th Floor, Imperial Bank Terraces Carl Cronje Drive Tyger Waterfront Bellville 7530', 1, 'Available', 0),
(3, 101, '29 De Havilland Crescent Pro Park, Building 1 Persequor Technopark Pretoria 0020', 3, 'Partially Completed', 0),
(4, 102, '5th Floor, Imperial Bank Terraces Carl Cronje Drive Tyger Waterfront Bellville 7530', 3, 'Available', 0),
(5, 201, '101 test address', 4, 'Available', 0);
对于用户:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`supervisor` int(10) unsigned NOT NULL,
`version_code` varchar(10) NOT NULL,
`is_interviewer` tinyint(1) NOT NULL DEFAULT '0',
`is_supervisor` tinyint(1) NOT NULL DEFAULT '0',
`surname` varchar(50) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`, `password`, `supervisor`, `version_code`, `is_interviewer`, `is_supervisor`, `surname`) VALUES
(1, 'Rynardt', 'q', 2, '2.2.0', 1, 0, ''),
(2, 'Herholdt', 'q', 0, '2.2.0', 0, 1, ''),
(3, 'test', 'test', 2, '2.2.0', 1, 0, ''),
(4, 'Botha', 'q', 2, '', 1, 0, '');
编辑 2 有关我正在尝试做的事情的更多信息:
如果您查看我所做的 EDIT,您将看到现在包含 SQL 表。u.supervisor 是操作类型整数,用于指示用户表中主管条目的 id。$username 是字符串类型,表示主管的名称。因此,我首先必须获取主管的 id,然后获取所有拥有具有先前找到的 id 的主管的用户的 id。然后使用这个 users.id 数组获取与这些用户关联的所有问卷,并在主管登录时将其显示给主管。