【发布时间】:2012-10-22 06:26:16
【问题描述】:
我有以下表格:
systems
-----------
id
name
price
online
productid
specifications
-------------
id
systemid
type
componentid
quantity
components
-------------
id
name
brand
type
description
我需要使用多个选项过滤这些表。每个系统都有多个规格行,每个“规格”行都链接到相应的“组件”行。
我的问题是这样的:
我需要能够根据表连接按多个属性过滤系统。我一直在使用允许我有 1 个搜索选项的代码,但没有更多:
select
`systems`.`id`,
`systems`.`name`,
`specifications`.`type`
from `systems`
join specifications
on `systems`.`id` = `specifications`.`systemid`
join components
on `specifications`.`componentid` = `components`.`id`
where
`specifications`.`type` = 'cpu'
and `components`.`brand` = 'amd'
所以,这将让我在规格类型为 CPU 且品牌为 AMD 的情况下进行连接,但如果我也添加其他要查找的内容,例如 specifications.type ='graphics' AND components.brand = 'nvidia',它将无法正常工作。我认为这是 join 工作方式所固有的,正如我所说,我无法在这里阐明这个问题,因为我对这些更复杂的数据库事务很陌生,并且非常感谢您指出正确的方向!
我使用 CodeIgniter 作为我的框架,我想尝试通过 MySQL 来了解这个问题,而不是在可能的情况下在 PHP 中进行 - 因为我想更好地了解正在发生的事情在这里。
【问题讨论】:
-
如何将
specifications.type ='graphics' AND components.brand = 'nvidia'添加到您的子句中?当你运行它时,你得到了什么结果? -
它与问题无关,但可能有用。我建议您将列 systemid 重命名为 systems_id。您可以使用像这样 (tablename_id) 这样命名的外键列轻松迁移到 DataMapper。
-
@andrewsi 我只是在底部添加了额外的“AND”语句,所以想象上面的语句带有“AND
specifications.type= 'graphics' andcomponents.brand= 'nvidia'。似乎 Darrrren 将 where 语句组合在一起的答案(如下)似乎朝着正确的方向发展,但在运行带有两个 where 子句的语句时不起作用。
标签: php mysql codeigniter join where-clause