mySQL左加入5个表?

时间:2018-05-03 23:41:06

标签: mysql

enter image description here先谢谢你的帮助。我在mySQL数据库中使用5个表。系统是这样的,我有一个顶级表名为"所有者" (客户)有本地业务(商店)。这些所有者出去在yelp(citation_sources)等网站上创建帐户,因此拥有登录凭证(citation_login)。一旦他们在引文来源上有一个帐户,他们就会在商店中添加商店。

我希望创建一个选择所有引用来源的查询,无论所有者是否拥有帐户,并循环记录集,显示他们拥有帐户的每个引用来源的登录信息,以及作为任何商店列表。

我的问题涉及在5个表上进行左连接。我遗漏了大部分字段,但设置了主键和外键是连接的顺序很重要,即。从一个特定的表开始,以另一个表结束?

我尝试了这个命令,但实际上有96个citation_sources它只能带回33行。

我想我明白了。我创建了一个名为" citation_shop"的新表。使用复合主键 - 引用 - 商店。然后我运行了一个查询,它让我获得了我追求的结果。我最后在第一个左连接中设置了一个条件。

从citation_sources中选择citation_sources.name,citation_shop.shop,在citation_sources.id上加入citation_shop = citation_shop.citation,在(6,7)中将citation_shop.shop留在citation_shop.shop = shops.id group by citation_sources.name ,citation_shop.shop限制100

CREATE TABLE `citation_shop` (
  `shop` smallint(5) UNSIGNED NOT NULL,
  `citation` smallint(6) UNSIGNED NOT NULL,
  `url` text NOT NULL,
  `count` smallint(3) UNSIGNED NOT NULL,
  `status` tinyint(1) UNSIGNED NOT NULL,
  `sort` tinyint(3) UNSIGNED NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `citation_shop`
--
ALTER TABLE `citation_shop`
  ADD PRIMARY KEY (`citation`,`shop`);

select owners.id as owner_id, shops.id as shop_id, citation_sources.name, citation_shop_urls.url, citation_logins.password 
from owners 
inner join shops on owners.id = shops.owner_id 
left join citation_logins on owners.id = citation_logins.owner 
left join citation_sources on citation_logins.c_source = citation_sources.id 
left join citation_shop_urls on citation_sources.id = citation_shop_urls.citation_id 
where owners.id = 3 
group by citation_sources.name

以下是我认为是相关性的表:

CREATE TABLE `owners` (
  `id` smallint(6) UNSIGNED NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

CREATE TABLE `shops` (
  `id` smallint(5) UNSIGNED NOT NULL,
  `title` varchar(50) DEFAULT '',
  `owner_id` smallint(5) UNSIGNED NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `citation_sources` (
  `id` smallint(6) UNSIGNED NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `citation_shop_urls` (
  `shop` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
  `citation_id` tinyint(5) UNSIGNED NOT NULL DEFAULT '0',
  `owner` smallint(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `citation_logins` (
  `c_source` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
  `owner` smallint(6) NOT NULL,
  `user_name` text NOT NULL,
  `password` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1 个答案:

答案 0 :(得分:0)

LEFT JOIN中,第一个表是获取所有行的表,即使它们在其他表中没有匹配也是如此。因此,如果您想要所有 citation_sources,即使那些与任何owner无关的人,那么citation_sources应该是LEFT JOIN左侧的表格

要仅将所有者信息过滤到id = 3,请将o.id = 3放入与ON相关联的owners子句中。然后使用WHERE子句删除所有其他行。

SELECT o.id as owner_id, s.id as shop_id, cs.name, u.url, cl.password 
FROM citation_sources AS cs
LEFT JOIN citation_shop_urls AS u ON u.citation_id = cs.id
LEFT JOIN citation_logins AS cl ON cs.id = cl.c_source
LEFT JOIN owners AS o ON o.id = cl.owner AND o.id = 3
LEFT JOIN shops AS s ON s.owner_id = o.id
WHERE o.id IS NULL OR o.id = 3
相关问题