MySQL选择为特殊而输出排除null版本

时间:2015-06-10 06:22:42

标签: mysql

这是我的sql语句,因为你可以看到选择SELECT类别为特殊,我怎么才能得到特殊的NOT NULL。

SELECT p.product_id, 

(SELECT price FROM oc_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' 
    AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()))   
    ORDER BY ps.priority 
    ASC, ps.price ASC LIMIT 1) AS special 

FROM oc_product_to_category p2c 
LEFT JOIN oc_product p ON (p2c.product_id = p.product_id) 

WHERE AND p.status = '1' AND p.date_available <= NOW() 

AND p2c.category_id = '20' 

GROUP BY p.product_id ORDER BY p.sort_order ASC LIMIT 0,5

使用上面的语句,我有以下输出: enter image description here

我尝试在date_available之后添加WHERE,然后我没有结果。我尝试使用special IS NOT NULL它显示:未知列&#39;特殊&#39;在&#39; where子句&#39;

任何人都可以帮忙解决这个问题? 谢谢!

4 个答案:

答案 0 :(得分:1)

特殊是一个alias名称,您将其分配给逻辑输出,这发生在执行where子句之后,因此特殊列未知。

试试这个::

Select * 
from
(
SELECT p.product_id, 

(SELECT price FROM oc_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' 
    AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()))   
    ORDER BY ps.priority 
    ASC, ps.price ASC LIMIT 1) AS special 

FROM oc_product_to_category p2c 
LEFT JOIN oc_product p ON (p2c.product_id = p.product_id) 

WHERE AND p.status = '1' AND p.date_available <= NOW() 

AND p2c.category_id = '20' 

GROUP BY p.product_id ORDER BY p.sort_order ASC 
) mytemp where mytemp.special is not null

LIMIT 0,5

答案 1 :(得分:1)

只需使用INNER JOIN代替LEFT JOIN

(SELECT price FROM oc_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' 
    AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()))   
    ORDER BY ps.priority 
    ASC, ps.price ASC LIMIT 1) AS special 

FROM oc_product_to_category p2c 
INNER JOIN oc_product p ON (p2c.product_id = p.product_id) 

WHERE AND p.status = '1' AND p.date_available <= NOW() 

AND p2c.category_id = '20' 

GROUP BY p.product_id ORDER BY p.sort_order ASC 
) mytemp where mytemp.special is not null

LIMIT 0,5

答案 2 :(得分:0)

您需要在别名上设置过滤器,如下所示: -

SELECT user_name AS SPL FROM `apc_users` 
WHERE 1 
HAVING SPL IS NOT NULL 
ORDER BY `apc_users`.`user_name` ASC

在你的情况下: -

SELECT p.product_id, 

(SELECT price FROM oc_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' 
    AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()))   
    ORDER BY ps.priority 
    ASC, ps.price ASC LIMIT 1) AS special 

FROM oc_product_to_category p2c 
LEFT JOIN oc_product p ON (p2c.product_id = p.product_id) 

WHERE AND p.status = '1' AND p.date_available <= NOW() 

AND p2c.category_id = '20' HAVING special IS NOT NULL

GROUP BY p.product_id ORDER BY p.sort_order ASC LIMIT 0,5

答案 3 :(得分:0)

试试这个真的很适合你

SELECT p.product_id, 

(SELECT price FROM oc_product_special ps WHERE ps.product_id = p.product_id AND price IS NOT NULL AND ps.customer_group_id = '1' 
    AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()))   
    ORDER BY ps.priority 
    ASC, ps.price ASC LIMIT 1) AS special 

FROM oc_product_to_category p2c 
LEFT JOIN oc_product p ON (p2c.product_id = p.product_id) 

WHERE AND p.status = '1' AND p.date_available <= NOW() 

AND p2c.category_id = '20' 

GROUP BY p.product_id ORDER BY p.sort_order ASC LIMIT 0,5