匹配数据库中的数据以显示正确的信息

时间:2012-12-30 00:34:14

标签: sql

我有3个不同的数据库,我想根据要求获取信息。

CREATE TABLE IF NOT EXISTS `catbreeds` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `information_breed` varchar(30) NOT NULL,
  `information_breed_seo` varchar(30) NOT NULL,
  `information_ems` varchar(4) NOT NULL,
  `information_ipaddress` text NOT NULL,
  `fact_head` text NOT NULL,
  `fact_ears` text NOT NULL,
  `fact_eyes` text NOT NULL,
  `fact_eyecolor` text NOT NULL,
  `fact_body` text NOT NULL,
  `fact_legs` text NOT NULL,
  `fact_paws` text NOT NULL,
  `fact_tail` text NOT NULL,
  `fact_coat` text NOT NULL,
  `fact_extra` text NOT NULL,
  `date_added` datetime NOT NULL,
  `date_edited` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
)

CREATE TABLE IF NOT EXISTS `catbreeds_personality` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `information_name` varchar(50) NOT NULL,
  `information_name_seo` varchar(50) NOT NULL,
  `information_ipaddress` text NOT NULL,
  `date_added` datetime NOT NULL,
  `date_edited` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
)

CREATE TABLE IF NOT EXISTS `catbreeds_personality_links` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `id_breed` int(10) DEFAULT '0',
  `id_personality` int(10) DEFAULT '0',
  `information_ipaddress` text NOT NULL,
  `date_linked` datetime NOT NULL,
  `date_edited` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
)

在源代码中,我循环来自catbreeds_personality数据库的个性。每个人都在网站上链接,当我点击每个链接时,带有catbreeds的列表(从catbreeds数据库中的列表中获取)将在我选择的个性之后对猫品种进行排序。


示例

catbreeds数据库中有2个猫品种; BurmillaAbessinier

catbreeds_personality数据库有4个人物; SocialPlayfulActiveCurious

catbreeds_personality_links数据库有3个链接的个性; SocialPlayfulCurious

在网站上,当访问者点击Cursious的链接时,品种Burmilla将显示,Abessinier将隐藏。如果点击了人格SocialPlayful,则会显示品种Abessinier,而其他品种会隐藏。等等。我希望你能了解这一切是如何运作的。


这里唯一的问题是我不知道SQL的样子。目前,我的SQL查询如下所示:

SELECT * FROM catbreeds AS cb, catbreeds_personality AS cbp, catbreeds_personality_links AS cbpl
WHERE cbp.id = '6'
AND cbpl.id_breed = cb.id
ORDER BY cb.information_breed ASC

SQL查询返回4个结果,这些结果甚至没有ID号6。

我该如何解决这个问题?

提前致谢。

2 个答案:

答案 0 :(得分:1)

SELECT * FROM catbreeds_personality_links AS cbpl
INNER JOIN catbreeds AS cb ON cbpl.id_breed = cb.id
INNER JOIN catbreeds_personality AS cbp ON cbpl.id_personality = cbp.id
WHERE cbp.id = '6'
ORDER BY cb.information_breed ASC

答案 1 :(得分:0)

Select * from catbreeds cb inner join catbreeds_personality_links cbpl on ( cb.id=cbpl.id_breed) inner join catbreeds_personality cbp on (cbpl.id_personality = cbp.id) where cbp.id = '6' order by cb.information_breed asc