0

次の表があります。

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'機能しないようです。私が言ったように、これは結合が機能する方法に固有のものだと思います.私はこれらのより複雑なデータベーストランザクションにまったく慣れていないので、ここで問題を明確にするのに苦労しています.

私はフレームワークとして CodeIgniter を使用しています。可能であれば、PHP で行うのではなく、MySQL を介してこれを理解したいと思います。ここで何が起こっているのかをよりよく理解したいからです。

4

2 に答える 2

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') 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'
)
于 2012-11-01T16:06:59.277 に答える
0

各クエリをサブクエリとして実行することで、うまくいきました。

そのため、AMD プロセッサを搭載したシステムのすべての ID を、NVIDIA グラフィックス カードを搭載したすべてのシステムを検索する条件付き IN 句に返します。

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` =  'graphics'
    AND  `components`.`brand` =  'nvidia'
    )
AND (
    `systems`.`id` IN (
          SELECT  `systems`.`id` 
          FROM  `systems` 
          JOIN specifications ON  `systems`.`id` =  `specifications`.`systemid` 
          JOIN components ON  `specifications`.`componentid` =  `components`.`id` 
     WHERE (
          `specifications`.`type` =  'cpu'
          AND  `components`.`brand` =  'amd'
          )
     )
 )

プログラム的には、私はそれが非常に面倒だと思います.効率的にどのように積み上げられるかはわかりません-私は主に独学のプログラマーであるため、常に「正しい」方法で物事を行っていることを確認しようとしています. この方法で実行すると問題が発生することはありますか? 代わりに CodeIgniter がその ID のセットを返すようにしたほうがよいでしょうか?

この質問はいくらか単純化されており、最終的にはいくつかのサブクエリが含まれることに注意してください-ただし、サイト自体に大きな負荷がかかることはありません.

于 2012-11-01T17:40:36.773 に答える