我想使用IF()函数将某些列值转换为列 和CONCAT()函数,但我不能以某种方式正确。 我运行这个sql之后:
SELECT
IF( meta_key = 'property_id', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_id,
IF( meta_key = 'property_contract', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_contract,
IF( meta_key = 'property_agents', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_agents
FROM wp_postmeta
WHERE property_id !='faslse'
and property_agents = 'John Doe'
我收到此错误: 1054 - 'where子句'中的未知列'property_id'
编辑:
我正在尝试此查询但返回0行...
select *
from
( select
IF( meta_key = 'property_id', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_id,
IF( meta_key = 'property_contract', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_contract,
IF( meta_key = 'property_agents', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_agents
from
wp_postmeta) p
WHERE
p.property_id != 'false'
and p.property_contract != 'false'
and p.property_agents!= 'false'
答案 0 :(得分:1)
别名在where子句之后应用,因此您有两个选择,重复IF子句:
select
IF( meta_key = 'property_id', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_id,
IF( meta_key = 'property_contract', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_contract,
IF( meta_key = 'property_agents', CONCAT(meta_key, ' ', meta_value), 'false' ) AS property_agents
from
wp_postmeta
where
IF( meta_key = 'property_id', CONCAT(meta_key, ' ', meta_value), 'false' ) !='faslse'
and IF( meta_key = 'property_agents', CONCAT(meta_key, ' ', meta_value), 'false' ) = 'John Doe'
或使用子查询:
select *
from
(
select
IF(...) as property_id,
IF(...) as property_agents
from
wp_postmeta
) s
where
s.property_id != 'false'
and s.property_agents = 'John Doe'
可以通过各种方式优化查询,但这取决于您的要求。
修改强>
从问题中不清楚,但我认为你想要解决的问题有点不同。我想你的post_id
表中有一个wp_postmeta
字段,我想每个帖子的属性都在不同的行中。通过以下查询,您可以获得单个帖子的ID,合同和代理:
select
post_id,
max(case when meta_key = 'property_id' then CONCAT(meta_key, ' ', meta_value) end) as property_id,
max(case when meta_key = 'property_contract' then CONCAT(meta_key, ' ', meta_value) end) as property_contract,
max(case when meta_key = 'property_id' then CONCAT(meta_key, ' ', meta_value) end) as property_agents
from
wp_postmeta
group by
post_id
然后您可以添加具有以下内容的过滤器:
having
property_agents='property_agents John Doe'
答案 1 :(得分:0)
而不是
WHERE property_id !='faslse'
请使用此功能,同时更正从faslse
到false
HAVING property_id !='faslse'
复制
您可以在GROUP BY,ORDER BY或HAVING子句中使用别名
答案 2 :(得分:0)
请代表“WHERE”条款使用“HAVING”条款
答案 3 :(得分:0)
问题就像@fthiella所说的价值观 因为我选择的是同一个,所以不能同时是非假的 用于执行IF()语句的列。所以经过一番思考后我想出了这个:
SELECT
p_id.property_id ,
p_agents.property_agents,
p_contract.property_contract
FROM
(SELECT post_id , IF( meta_key = 'property_contract', meta_value , 'false' ) AS property_contract FROM wp_postmeta HAVING property_contract != 'false' )p_contract
INNER join
(SELECT post_id , IF( meta_key = 'property_agents', meta_value , 'false' ) AS property_agents FROM wp_postmeta HAVING property_agents != 'false' )p_agents
ON
p_contract.post_id = p_agents.post_id
INNER join
(SELECT post_id , IF( meta_key = 'property_id', meta_value , 'false' ) AS property_id FROM wp_postmeta HAVING property_id != 'false' )p_id
ON
p_contract.post_id = p_id.post_id
GROUP BY p_contract.post_id Order by p_contract.post_id
它有效。谢谢大家的努力和帮助。