mysql查询使用别名为搜索条件

SELECT
	a.id,
	a. STATUS,
	a.sign_photo,
	GROUP_CONCAT(sfr.file_path SEPARATOR ';') AS 'resource_id'
FROM
	order_sale a
LEFT JOIN order_logistic_data old ON a.id = old.sale_id
LEFT JOIN order_logistic_nodes oln ON oln.id = old.logistic_node_id
AND oln.node_name = '签收'
LEFT JOIN order_logistic_resource olr ON olr.logistic_data_id = old.id
LEFT JOIN sys_file_resource sfr ON olr.resource_id = sfr.id
WHERE
	a.carrier_id = '5379897b9e7e48fc92e4335599eeaabd'
AND a.create_date >= '2018-11-21 16:03:30.0 '
AND a.create_date <= '2018-12-06 16:03:30.0'
AND a.del_flag = 0
AND a.del_flag = 0
GROUP BY
	a.id
ORDER BY
	a.create_date DESC
LIMIT 10

不添加别名搜素条件时查询结果都是正确的

但是只要添加上and resource is  null,resource_id一栏本该有值的也为null

通过官网发现mysql不允许使用列别名作为查询条件,最终可以使用HAVING来实现该有的查询结果

SELECT
	a.id,
	a. STATUS,
	a.sign_photo,
	GROUP_CONCAT(sfr.file_path SEPARATOR ';') AS 'resource_id'
FROM
	order_sale a
LEFT JOIN order_logistic_data old ON a.id = old.sale_id
LEFT JOIN order_logistic_nodes oln ON oln.id = old.logistic_node_id
AND oln.node_name = '签收'
LEFT JOIN order_logistic_resource olr ON olr.logistic_data_id = old.id
LEFT JOIN sys_file_resource sfr ON olr.resource_id = sfr.id
WHERE
	a.carrier_id = '5379897b9e7e48fc92e4335599eeaabd'
AND a.create_date >= '2018-11-21 16:03:30.0 '
AND a.create_date <= '2018-12-06 16:03:30.0'
AND a.del_flag = 0
AND a.del_flag = 0
GROUP BY
	a.id
HAVING
	resource_id IS NULL
ORDER BY
	a.create_date DESC
LIMIT 10

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值