我有以下mysql查询
SELECT cibor_users.ID,
cibor_users.display_name,
cibor_users.user_email,
cibor_users.user_url,
GROUP_CONCAT(meta_value) AS customer_address
FROM cibor_users
JOIN cibor_usermeta ON cibor_users.ID = cibor_usermeta.user_id
WHERE cibor_usermeta.meta_key IN ('paupress_address_one_1','paupress_address_city_1','paupress_address_state_1','paupress_address_postal_code_1','telephone','user_type','primary_business','realtor_designation')
GROUP BY cibor_usermeta.user_id
然后使用以下信息在表格中显示信息:
<td ><?php echo $row['display_name'];?></td>
<td ><?php echo $row['user_url'];?></td>
<td ><?php echo $row['user_email'];?></td>
<td ><?php echo $row['customer_address'];?></td>
问题是“WHERE”语句中的值是随机且不一致地显示的。所以我添加了一个ORDER BY语句:
SELECT cibor_users.ID,
cibor_users.display_name,
cibor_users.user_email,
cibor_users.user_url,
GROUP_CONCAT(meta_value ORDER BY meta_value ASC SEPARATOR ' ') AS customer_address
FROM cibor_users
JOIN cibor_usermeta ON cibor_users.ID = cibor_usermeta.user_id
WHERE cibor_usermeta.meta_key IN ('paupress_address_one_1','paupress_address_city_1','paupress_address_state_1','paupress_address_postal_code_1','telephone','user_type','primary_business','realtor_designation')
GROUP BY cibor_usermeta.user_id
但是,这会按字母顺序排列“Customer_Address”所需的信息。我需要显示的信息与它们放在“WHERE”语句中的顺序相同。
我尝试使用ORDER BY FIELD
WHERE cibor_usermeta.meta_key IN ('paupress_address_one_1','paupress_address_city_1','paupress_address_state_1','paupress_address_postal_code_1','telephone','user_type','primary_business','realtor_designation')
ORDER BY FIELD (cibor_usermeta.meta_key, ['paupress_address_one_1','paupress_address_city_1','paupress_address_state_1','paupress_address_postal_code_1','telephone','user_type','primary_business','realtor_designation'])
GROUP BY cibor_usermeta.user_id
有没有办法根据WHERE IN语句指定连接信息的确切顺序?
答案 0 :(得分:1)
在订单条款
中你必须做一个案例group_concat(meta_value
order by
case meta_key when 'paupress_adress_one1' then 0
when 'paupress_address_city_1' then 1
when 'paupress_address_state_1' then 2
--etc.
end
separator ' ')