在一个结果中加入更多表

时间:2012-03-02 08:02:11

标签: php sql join

我需要帮助sql select from multiply tables。

表:

s_products
- id
- name
- url
- brand_id

s_products_categories
- product_id
- category_id

s_brands
- id
- name
- url

s_categories
- id
- name
- url

比较

s_products.id =  s_products_categories.product_id
s_categories.id = s_products_categories.category_id
s_products.brand_id = s_brands.id

结果需要:

1)$products['s_categories.url']['s_brands.url']['s_products.url'] = 's_products.name';

2)$brands['s_categories.url']['s_brands.url'] = 's_brands.name';

你能帮助我在一个选择(或更多)中做到吗?

3 个答案:

答案 0 :(得分:1)

select *yourcolumnsname*
from s_products INNER JOIN  s_products_categories
on s_products.id =  s_products_categories.product_id
INNER JOIN s_categories.id 
on s_categories.id = s_products_categories.category_id;

答案 1 :(得分:1)

SELECT s_categories.url as url1, s_products.url as url2, s_products.name as name 
FROM s_products 
INNER JOIN s_products_categories on s_products.id   = s_products_categories.product_id 
INNER JOIN s_categories          on s_categories.id = s_products_categories.category_id

答案 2 :(得分:1)

尝试:

select c.url  category_url,
       c.name category_name,
       b.url  brand_url,
       b.name brand_name,
       p.url  product_url,
       p.name product_name
from s_products p
join s_brands b on p.brand_id = b.id
join s_products_categories pc on p.id = pc.product_id
join s_categories c on pc.category_id = c.id

要查看特定字段的组合(例如,category_url与brand_url),请在distinct之后立即添加select,并删除不需要的字段。