我需要加入吗?

时间:2012-02-15 15:12:22

标签: mysql sql

我有3个表:products,products_description,products_to_categories

这是我的sql:

SELECT `products`.`products_model`, 
       `products_description`.`products_name` 
FROM   products, 
       products_description, 
       products_to_categories 
WHERE  `products`.`products_id` = `products_description`.`products_id` = 
                                         `products_to_categories`. `products_id` 
       AND `products_to_categories`.`categories_id` <> '91' 
ORDER  BY `products`.`products_model` DESC 

所有三个表都有products_id作为主键,我试图基于此链接所有三个。我想我需要加入,因为我似乎无法让所有三个在一个声明中相等?

6 个答案:

答案 0 :(得分:2)

你可以通过JOIN来做到这一点,但是如果你想使用Where语句那么好,但是你必须使用 AND 来实现它

SELECT `products`.`products_model`, `products_description`.`products_name`
FROM products, products_description, products_to_categories
WHERE `products`.`products_id` =  `products_description`.`products_id` 
   AND `products`.`products_id`= `products_to_categories`.`products_id` 
   AND `products_to_categories`.`categories_id` <> '91' 
ORDER BY `products`.`products_model` DESC

答案 1 :(得分:1)

select p.`products_model`,
    pd.`products_name`
from products p
inner join products_description pd on p.`products_id` = pd.`products_id`
inner join products_to_categories pc on p.`products_id` = pc.`products_id`
where pc.`categories_id` <> '91'
order by p.`products_model` desc

答案 2 :(得分:0)

尝试

SELECT products_model, products_name
  FROM products
       NATURAL JOIN products_description
       NATURAL JOIN products_to_categories;

答案 3 :(得分:0)

无论如何,更现代的JOIN通常比旧的隐式连接语法更受欢迎。它提供了关于表格如何相关的更具可读性的图片,并通过仅包括WHERE中的实际限制条件来允许更清晰的WHERE子句。

SELECT 
  `products`.`products_model`,
  `products_description`.`products_name`
FROM 
  products
  JOIN products_description ON products.product_id = products_description.product_id
  JOIN products_to_categories ON products.product_id = products_to_categories.product_id
WHERE  `products_to_categories`.`categories_id` <> '91' 
ORDER BY `products`.`products_model` DESC

答案 4 :(得分:0)

尽量不要在FROM子句中使用逗号。它是sql标准的更旧版本的延续,并不再被认为是一个好习惯。几乎就在您使用TableA,TableB语法的任何地方,您应该学会使用TableA INNER JOIN TableB ON代替。

在这种情况下,你可以像这样逃避:

SELECT `products`.`products_model`, `products_description`.`products_name`
FROM products, products_description, products_to_categories
WHERE `products`.`products_id` =  `products_description`.`products_id` 
    AND `products`.`products_id` = `products_to_categories`. `products_id` 
    AND `products_to_categories`.`categories_id` <> '91' 
ORDER BY `products`.`products_model` DESC

但是,你真的应该学会这样写:

SELECT `products`.`products_model`, `products_description`.`products_name`
FROM products
INNER JOIN products_description ON products.products_id = `products_description`.`products_id`
INNER JOIN `products_to_categories` ON `products_to_categories`.`products_id` = `products`.`products_id`
WHERE `products_to_categories`.`categories_id` <> '91' 
ORDER BY `products`.`products_model` DESC

答案 5 :(得分:0)

    SELECT `products`.`products_model`, `products_description`.`products_name`

FROM products,products_description,products_to_categories 在productsproducts_id = products_descriptionproducts_idproducts_to_categoriesproducts_id = productsproducts_idproducts_descriptionproducts_id = products_to_categoriesproducts_id  products_to_categoriesproducts_idproducts_to_categoriescategories_id&lt;&gt; '91' ORDER BY productsproducts_model DESC

试试这个我觉得它比你的查询更好用