MySQL选择为special,输出排除空版本

MySQL选择为special,输出排除空版本,mysql,Mysql,这是我的sql语句,因为您可以看到select类别为special,我如何才能只获取special不为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 < NO

这是我的sql语句,因为您可以看到select类别为special,我如何才能只获取special不为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
通过上述陈述,我有以下输出:

我尝试在可用的日期后添加WHERE,但没有结果。我尝试使用special不为NULL,它显示:“where子句”中的未知列“special”

在这件事上谁能帮忙? 谢谢

special是一个别名,您正在将它分配给逻辑的输出,这发生在执行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

您需要在alias上设置筛选器,如下所示:-

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
只需使用内部连接而不是左连接


试试看,这真的对你有用

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

不确定,为什么人们投票否决你,我发现你的解决方案是最好的,并且解释得很好。谢谢其他也谢谢你的投入@Shiro:很好,它很有帮助。这个查询不会解决问题,事实上它有语法错误???看起来你从我的解决方案中复制了…@SashiKant我没有复制你的答案。内部联接必须用于此目的,因为它除了空值之外。现在,根据上帝的创造,我们是人类。。。正当因此,作为一个人,我们应该使用我们的大脑,我们必须记住,在成为程序员之前,我们是人。。因此,随着你知识的增长,请增加你的幽默感和湿度。。。。对,兄弟,永远不要低估任何人。。。如果我的评论伤害了你,那么我很抱歉。。。你可以给我负面的评价,我不在乎。。。但我只是说实话…奇怪的是,我们都想到了一个相同的别名。。。但我担心的是,您的查询无法运行,请重新检查
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