gpt4 book ai didi

php - 使用连接表中的值连接多个表

转载 作者:行者123 更新时间:2023-11-29 14:12:52 24 4
gpt4 key购买 nike

我有以下表格:

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 组件。 brand = 'nvidia' 它就是行不通。我认为这是联接工作方式所固有的,正如我所说,我在这里很难阐明这个问题,因为我对这些更复杂的数据库事务还很陌生,并且非常感谢您指出正确的方向!

我使用 CodeIgniter 作为我的框架,如果可能的话,我想尝试通过 MySQL 来了解这一点,而不是使用 PHP - 因为我想更好地了解正在发生的事情在这里。

最佳答案

你的意思是这样

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') OR
(`specifications`.`type` = `graphics` AND `components`.`brand` = `nvidia`)

不起作用?

这样的事情怎么样

SELECT S.`id`, S.`name`, P.`type` FROM `systems` S 
JOIN `specifications` P ON S.`id` = P.`systemid`
WHERE S.`id` IN (

SELECT S2.`systemid` AS id FROM `specifications` S2
JOIN `components` C2 ON S2.`componentid` = C2.`id`
WHERE S2.`type` = 'cpu' AND c2.`brand` = 'amd'
) AND S.`id` IN (

SELECT S3.`systemid` AS id FROM `specifications` S3
JOIN `components` C3 ON S3.`componentid` = C3.`id`
WHERE S3.`type` = 'graphics' AND c3.`brand` = 'nvidia'
)

关于php - 使用连接表中的值连接多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13180935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com