-5

私のプロジェクト (オンライン ショッピング Web サイト) には、flipkart Web サイトのような検索ボックスがあります。検索キーワードを入力すると、4 つのテーブルの結果がページに表示されます。

Table : `tbl_product`
Field : `id`,`date`,`title`,`model_no`,`quantity`,`category_id`,`brand_id`,`color`,`size`

Table : `tbl_brand`
Field : `id`,`date`,`brand_name`,`category_id`,`description`

Table : `tbl_category`
Field : `id`,`date`,`category_title`, `description`

Table : `tbl_product_specification`
Field : `id`, `date`, `product_id`,`specification_title`

今、 $keyword 変数の検索結果を次のように取得したい:

`title`,`model_no` from `tbl_product`
`brand_name` from `tbl_brand`
`category_title` from `tbl_category`
`specification_title` from `tbl_product_specification`

MySQL SQL クエリが苦手です。では、これを完了するのはどの SQL でしょうか?

4

2 に答える 2

0

ユニオンを使用して、異なるテーブルからのクエリを結合する必要があります。何かのようなもの:

select title, model_no 
from tbl_product
where title like 'mykeyword'
union
select brand_name, ''
from tbl_brand
where brand_name like 'mykeyword'
select category_title, ''
from tbl_category
where category_title like 'mykeyword'
union 
select specification_title, ''
from tbl_product_specification
where specification_title like 'mykeyword'
order by 1

注意: Unionの詳細については、個別の各クエリが同じ数の列と同じデータ型を同じ順序で返す必要があります。

于 2014-08-12T22:03:11.487 に答える