通过循环 ID 从 3 个表获取数据
我有一个具有此结构的 3 个表的数据库,
CREATE TABLE `mailers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mailer_title` varchar(150) NOT NULL,
`mailer_header` varchar(60) NOT NULL,
`mailer_type` enum('single','multi') NOT NULL,
`introduction` varchar(80) NOT NULL,
`status` enum('live','dead','draft') NOT NULL,
`flag` enum('sent','unsent') NOT NULL,
`date_mailer_created` int(11) NOT NULL,
`date_mailer_updated` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;
CREATE TABLE `mailer_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`headline` varchar(320) NOT NULL,
`content` text NOT NULL,
`mailer_id` int(11) NOT NULL,
`position` enum('left','right','centre') DEFAULT NULL,
`tab_1_name` varchar(25) DEFAULT NULL,
`tab_1_link` varchar(250) DEFAULT NULL,
`tab_2_name` varchar(25) DEFAULT NULL,
`tab_2_link` varchar(250) DEFAULT NULL,
`tab_3_name` varchar(25) DEFAULT NULL,
`tab_3_link` varchar(250) DEFAULT NULL,
`tab_4_name` varchar(25) DEFAULT NULL,
`tab_4_link` varchar(250) DEFAULT NULL,
`created_at` int(10) NOT NULL,
`updated_at` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `mailer_id` (`mailer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
CREATE TABLE `mailer_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(150) NOT NULL,
`filename` varchar(150) NOT NULL,
`mailer_id` int(11) NOT NULL,
`content_id` int(11) DEFAULT NULL,
`date_created` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=92 ;
ALTER TABLE `mailer_content`
ADD CONSTRAINT `mailer_content_ibfk_1`
FOREIGN KEY (`mailer_id`)
REFERENCES `mailers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
如何从 id 和 mailer 表为 47 的所有列中获取所有数据?我目前有这个sql,我目前有这个sql,但它没有返回正确的数据,
SELECT *
FROM `mailers`
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailers`.`id` = 47
I have a database with 3 tables in this structure,
CREATE TABLE `mailers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mailer_title` varchar(150) NOT NULL,
`mailer_header` varchar(60) NOT NULL,
`mailer_type` enum('single','multi') NOT NULL,
`introduction` varchar(80) NOT NULL,
`status` enum('live','dead','draft') NOT NULL,
`flag` enum('sent','unsent') NOT NULL,
`date_mailer_created` int(11) NOT NULL,
`date_mailer_updated` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;
CREATE TABLE `mailer_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`headline` varchar(320) NOT NULL,
`content` text NOT NULL,
`mailer_id` int(11) NOT NULL,
`position` enum('left','right','centre') DEFAULT NULL,
`tab_1_name` varchar(25) DEFAULT NULL,
`tab_1_link` varchar(250) DEFAULT NULL,
`tab_2_name` varchar(25) DEFAULT NULL,
`tab_2_link` varchar(250) DEFAULT NULL,
`tab_3_name` varchar(25) DEFAULT NULL,
`tab_3_link` varchar(250) DEFAULT NULL,
`tab_4_name` varchar(25) DEFAULT NULL,
`tab_4_link` varchar(250) DEFAULT NULL,
`created_at` int(10) NOT NULL,
`updated_at` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `mailer_id` (`mailer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
CREATE TABLE `mailer_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(150) NOT NULL,
`filename` varchar(150) NOT NULL,
`mailer_id` int(11) NOT NULL,
`content_id` int(11) DEFAULT NULL,
`date_created` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=92 ;
ALTER TABLE `mailer_content`
ADD CONSTRAINT `mailer_content_ibfk_1`
FOREIGN KEY (`mailer_id`)
REFERENCES `mailers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
How would I get all the data from all columns where the id and the mailer table is 47? I currently have this sql, I currently have this sql, but it is not returning the correct data,
SELECT *
FROM `mailers`
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
WHERE `mailers`.`id` = 47
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您省略了 mailer_images 上的连接条件:
You omitted a join condition on mailer_images:
试试这个
try this