0

朋友,两桌一桌是

CREATE TABLE `vbw_push_notifications` (
  `push_notification_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key ,Auto increment field',
  `push_notification_customer_ids` text NOT NULL COMMENT 'comma separated customer id list which was used for messaging/related customers/broadcasting',
  `push_notification_message` varchar(500) NOT NULL COMMENT 'The notification message.(A new message from Veebow/A new message from <Merchant Name>/A new public deal <Deal Name> from <Merchant Name>/A new game deal <Deal Name> from <Merchant Name>',
  `push_notification_time` datetime NOT NULL,
  `push_notification_is_processed` tinyint(4) NOT NULL,
  PRIMARY KEY (`push_notification_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COMMENT='The comma separated customer ids of the customers who needs ';

-- ----------------------------
-- Records of vbw_push_notifications
-- ----------------------------
INSERT INTO `vbw_push_notifications` VALUES ('1', '165836,65802,65829,65837,65838', 'test test test', '2013-11-07 12:36:42', '0');

我还有另一个表格,其中包含以下详细信息。

  CREATE TABLE `vbw_mobile_sessions` (
  `mobile_session_id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'The unique identifier for a mobile session',
  `mobile_session_start_time` datetime DEFAULT NULL COMMENT 'The starting time @ server of a mobile session',
  `mobile_session_end_time` datetime DEFAULT NULL COMMENT 'The ending time @ server of a mobile session',
  `mobile_session_token` varchar(255) DEFAULT NULL COMMENT 'The mobile session token generated for this session',
  `mobile_session_device_id` varchar(50) DEFAULT NULL COMMENT 'The device id of the device used for making the session',
  `mobile_session_customer_id` int(10) DEFAULT NULL COMMENT 'The customer ID of the customer who made this session',
  `mobile_session_device_type` tinyint(4) DEFAULT NULL COMMENT 'The type of device that customer uses for this session. 0 - iOS, 1 - Android',
  PRIMARY KEY (`mobile_session_id`),
  KEY `fk_mobile_session_customer_id` (`mobile_session_customer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=677 DEFAULT CHARSET=latin1 COMMENT='This table holds the merchant account activation links creat';

我想使用这样的子查询。

SELECT DISTINCT(mobile_session_customer_id)
FROM vbw_mobile_sessions
WHERE mobile_session_end_time IS null
AND mobile_session_customer_id IN (SELECT push_notification_customer_ids FROM vbw_push_notifications WHERE push_notification_id=6) .

此查询不返回零行。但是当我这样使用时,我得到了结果。

    SELECT DISTINCT(mobile_session_customer_id)
FROM vbw_mobile_sessions
WHERE mobile_session_end_time IS null
AND mobile_session_customer_id IN ( SELECT DISTINCT(mobile_session_customer_id)
FROM vbw_mobile_sessions
WHERE mobile_session_end_time IS null
AND mobile_session_customer_id IN (65836,65802,65829,65837,65838)

我认为子查询返回结果的格式不同。你能指出我犯的错误吗?非常感谢。

4

2 回答 2

1

你的答案很明显。字符串与 一组值'a,b,c,d'无关。这不是它的工作方式。 (a,b,c,d)

正确的解决方案是不在一个字段中使用分隔符分隔的值。您应该规范您的数据库结构并创建链接表。然后将您的值放入其中并使用子查询构建您的查询,从中进行选择。

另一种可能的解决方案是从应用程序中的字段中选择您的数据(字符串数据),通过分隔符将其拆分,然后替换为另一个查询。

于 2013-11-07T07:26:13.097 回答
0

子查询为您返回一个带有值的 varchar,'1,2,3'您需要一组整数,即1,2,3...

引擎将您的子查询结果视为 varchar()而不是一组整数。

你可以通过这个问题,确切地问你需要什么

于 2013-11-07T07:25:42.687 回答