0

我有 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)将在我选择的性格之后对猫品种进行排序。


例子

数据库中有2个猫品种catbreedsBurmillaAbessinier

数据库有4个catbreeds_personality个性;Social, Playful,ActiveCurious.

catbreeds_personality_links数据库有 3 个关联的人物;Social, Playful, 和Curious.

在网站上,当访问者点击该品种的链接时,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。

我怎么解决这个问题?

提前致谢。

4

2 回答 2

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
于 2012-12-30T00:51:51.610 回答
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

于 2012-12-30T00:57:20.197 回答