通过循环 ID 从 3 个表获取数据

发布于 2024-09-16 07:46:17 字数 2105 浏览 3 评论 0原文

我有一个具有此结构的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2024-09-23 07:46:17

您省略了 mailer_images 上的连接条件:

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`
                         AND `mailers`.`id` = `mailer_images`.`mailer_id` 
    WHERE `mailers`.`id` = 47

You omitted a join condition on mailer_images:

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`
                         AND `mailers`.`id` = `mailer_images`.`mailer_id` 
    WHERE `mailers`.`id` = 47
谈下烟灰 2024-09-23 07:46:17

试试这个

   SELECT *
     FROM `mailers` 
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id` 
LEFT JOIN `mailer_images` ON `mailer_images`.`mailer_id` = `mailer`.`id` 
    WHERE `mailers`.`id` = 47

try this

   SELECT *
     FROM `mailers` 
LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id` 
LEFT JOIN `mailer_images` ON `mailer_images`.`mailer_id` = `mailer`.`id` 
    WHERE `mailers`.`id` = 47
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文