Mysql查询中的问题

时间:2017-04-27 10:14:00

标签: php mysql

我在一次查询中尝试检查相同的字段。我的查询是这样的

SELECT DISTINCT products.products_id,products_description.products_title, products_sizes.size_id, products_sizes.size_title,products_sku.sku     
FROM products_description
LEFT JOIN products ON products_description.products_id = products.products_id
LEFT JOIN products_sizes ON products_description.products_id = products_sizes.products_id
LEFT JOIN product_stock_history ON product_stock_history.products_id = products.products_id
LEFT JOIN products_sku ON products_sku.size_id = products_sizes.size_id
WHERE products.enable_stock_management = '1'
AND products.products_id != '-11'

这将检索如下结果

enter image description here

但我想通过检查sizeid是否不同然后只显示来检索下面的详细信息,否则检索第一条记录

enter image description here

这可能实现吗?

2 个答案:

答案 0 :(得分:1)

    SELECT DISTINCT products.products_id,products_description.products_title, products_sizes.size_id, products_sizes.size_title,products_sku.sku     
FROM products_description
LEFT JOIN products ON products_description.products_id = products.products_id
LEFT JOIN products_sizes ON products_description.products_id = products_sizes.products_id
LEFT JOIN product_stock_history ON product_stock_history.products_id = products.products_id
LEFT JOIN products_sku ON products_sku.size_id = products_sizes.size_id
WHERE products.enable_stock_management = '1'
AND products.products_id != '-11'GROUP BY products.products_id; 

答案 1 :(得分:1)

我认为这会按预期工作

SELECT DISTINCT products.products_id,
                products_description.products_title,
                products_sizes.size_id,
                products_sizes.size_title,
                products_sku.sku
FROM products_description
LEFT JOIN products ON products_description.products_id = products.products_id
LEFT JOIN products_sizes ON products_description.products_id = products_sizes.products_id
LEFT JOIN product_stock_history ON product_stock_history.products_id = products.products_id
LEFT JOIN products_sku ON products_sku.size_id = products_sizes.size_id
WHERE products.enable_stock_management = '1'
  AND products.products_id != '-11'
GROUP BY size_id,
         size_title