【发布时间】:2018-05-28 06:47:25
【问题描述】:
我希望有人可以就问题所在提出建议。
SELECT sms.id FROM sms INNER JOIN screens ON sms.screen_id = screens.id WHERE screens.experience_id = '108';
与
SELECT id FROM sms
WHERE screen_id IN (SELECT id FROM screens WHERE experience_id = 108 )
...返回一个空行,但有些行应该返回。
澄清一下……
SELECT id FROM sms
... 返回 SMS 中的所有行
SELECT id FROM sms
WHERE screen_id IN (SELECT id FROM screens)
... 返回 SMS 中的所有行和
SELECT id FROM screens WHERE experience_id = 108
... 返回屏幕中具有该 ID 的所有行。
创建表格的 SQL...
CREATE TABLE IF NOT EXISTS `screens` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`device_id` int(11) NOT NULL,
`type` varchar(32) NOT NULL,
`description` text NOT NULL,
`position` int(11) NOT NULL,
`experience_id` int(11) NOT NULL,
`image` varchar(512) NOT NULL,
`url` varchar(512) NOT NULL,
`persona_id` int(11) NOT NULL,
`socialPost` text NOT NULL,
`socialLinkHeadline` varchar(512) NOT NULL,
`socialLinkDescription` text NOT NULL,
`socialLink` varchar(512) NOT NULL,
`socialAction` varchar(64) NOT NULL,
`bgcolor` varchar(16) NOT NULL DEFAULT '#fff',
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=472 ;
CREATE TABLE IF NOT EXISTS `sms` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source` varchar(16) NOT NULL,
`content` text NOT NULL,
`position` int(11) NOT NULL,
`screen_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
我运行这些查询以验证的 GIF 动图在这里:https://ibb.co/gzPFsd
我很茫然,可以看到人不能复制,谢谢!
【问题讨论】:
-
添加一些示例表数据和预期结果 - 作为格式化文本,而不是图像。
-
也许可以阅读 JOINs
-
我在帖子中添加了更多有用的信息,谢谢