简体   繁体   English

MySQL查询IN GROUP_CONCAT不起作用

[英]MySQL query IN GROUP_CONCAT not working

Having problems with a MySQL query. MySQL查询出现问题。 Only returning the results for the first item in the resulting GROUP_CONCAT array. 仅返回结果GROUP_CONCAT数组中第一项的结果。 ie resulting array is [1,3,4], the quesitionnaires for only user id 1 is returned. 例如,结果数组为[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."'))";

Am I using GROUP_CONCAT incorrectly? 我使用的GROUP_CONCAT错误吗? Is there a better way of doing this? 有更好的方法吗?

EDIT 1 Here are the SQL tables used in this query 编辑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);

And for users: 对于用户:

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, '');

EDIT 2 More info on what I am trying to do: 编辑2我正在尝试做的更多信息:

If you look at the EDIT I made you will see the SQL tables are now included. 如果您看一下我所做的编辑,您将看到现在包含了SQL表。 u.supervisor is op type integer to indicate id of supervisor entry in users table. u.supervisor是op类型的整数,用于表示用户表中主管条目的ID。 $username is of type string and indicates the name of the supervisor. $ username是字符串类型,表示主管的名称。 I therefore first have to get the id of the supervisor, then get all the id's of users that has a supervisor with the id found previously. 因此,我首先必须获取主管的ID,然后获取具有具有先前找到的ID的主管的用户的所有ID。 Then using this array of users.id get all the questionnaires associated with these users and display it to the supervisor when he logs in. 然后使用此users.id数组获取与这些用户相关的所有问卷,并在主管登录时将其显示给主管。

SELECT GROUP_CONCAT(id) FROM users WHERE supervisor = 
   (SELECT id FROM users WHERE username = '".$username."') 
GROUP BY supervisor

to work with aggregate functions like group_concat , you need to have an aggregated result set ;) 要使用诸如group_concat类的聚合函数,您需要具有聚合结果集;)

edit...hmm... in this special case 编辑...嗯...在这种特殊情况下

 WHERE ... IN (SELECT id FROM ...)

should be sufficent, no need to concatenate the result sets 应该足够,无需连接结果集

Well firstly, you could skip the GROUP_CONCAT(ID) here completely and simply use interviewer_id IN ( SELECT ID FROM users [...] ) 好吧,首先,您可以在此处完全跳过GROUP_CONCAT(ID),而只需使用auditer_id IN(从用户[...]选择SELECT)

Edit: since you updated your post with the tables, yep - then most likely you would need that Subquery. 编辑:由于您使用表格更新了帖子,是的-那么很可能您将需要该子查询。 You could also extract the ID of the supervisor in an additional query and could skip the nested-sql here. 您还可以在其他查​​询中提取主管的ID,并且可以在此处跳过嵌套SQL。 Or, since you said the supervisor logs in and has to see the data aquired... maybe also save the user-id into your session ( I guess it comes from there? ) and skip the subquery and additional query completely just by adding the user-id to your login-script creating session vars. 或者,因为您说过主管登录并必须查看所需的数据……也许还可以将用户ID保存到您的会话中(我想它是从那里来的?),而只需添加您的登录脚本创建会话变量的用户ID。

And just on a side note... you should at least hash the passwords with md5 or sha. 顺便提一下...您至少应该使用md5或sha哈希密码。 Good idea would be to save the time the user registred and use the timestamp + a constant to salt the password before hashing and saving password to the table. 好的主意是节省用户注册的时间,并使用时间戳+常量在散列并将密码保存到表之前对密码加盐。

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

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