I am working on a query to flatten some wp_postsmeta data in a WordPress database and need to set a specific value on various meta_values. I have a key of category and various category values, for each value, I want to set a column named color with a named color for our brand palette
select post_title as title,
MAX(CASE WHEN meta_key='corporate_calendar_category' THEN meta_value END) as 'category',
MAX(CASE WHEN meta_key = 'corporate_calendar_subcategory' THEN meta_value END) as 'subcategory',
// Do I need to include a nested CASE WHEN?
MAX(CASE WHEN meta_key = 'corporate_calendar_subcategory' and meta_value = 'Marketing' THEN 'blueLagoon' END) as 'color',
MAX(CASE WHEN meta_key = 'corporate_calendar_presenter' THEN meta_value END) as 'presenter',
MAX(CASE WHEN meta_key = 'corporate_calendar_date' THEN meta_value END) as 'start_date',
MAX(CASE WHEN meta_key = 'corporate_calendar_time' THEN meta_value END) as 'start_time',
MAX(CASE WHEN meta_key = 'corporate_calendar_duration' THEN meta_value END) as 'duration',
MAX(CASE WHEN meta_key = 'corporate_calendar_registration_link' THEN meta_value END) as 'registration_link',
MAX(CASE WHEN meta_key = 'corporate_calendar_description' THEN meta_value END) as 'description',
MAX(CASE WHEN meta_key = 'corporate_calendar_image_path' THEN meta_value END) as 'image_path'
FROM wp_posts p
JOIN wp_postmeta m ON p.id = m.post_id
where p.post_type = 'calendar-event'
and p.post_status = 'publish'
GROUP BY p.id
corporate_calendar_subcategory has many values, Marketing, HR, Company Holiday, etc, and for each subcategory I want each row to have a specific color.
title category subcategory color presenter start_date etc.
Example Training Marketing blueLagoon someone 08/29/2018
Labor Day Reminder Company Holiday camelot 09/03/2018
etc
etc
Is the best way to achieve this to use a nested CASE WHEN against the meta_value? Or is there a better way?
I also tried including an if statement (below) but that duplicated each row.
if(meta_key = 'corporate_calendar_subcategory',
CASE
WHEN meta_value = 'Marketing' THEn 'blueLagoon'
WHEN meta_value = 'Company Holiday' THEN 'camelot'
END,
'') as color,
Yes, the nested CASE statement would be the right way to go if you know all the categories and colors beforehand. If the category-colors are in another table, you can do either a subselect or a JOIN instead.
Here's an example with nested case:
max(case when meta_key = 'corporate_calendar_subcategory' then
case meta_value
when 'Marketing' then 'blue'
when 'Sales' then 'yellow
when 'Development' then 'red'
end
end)
Related
This question already has answers here:
Using column alias in WHERE clause of MySQL query produces an error
(7 answers)
Closed 4 years ago.
My query like this :
SELECT a.number, a.description, b.attribute_code, b.attribute_value
FROM items a
JOIN attr_maps b ON b.number = a.number WHERE a.number = AB123
If the query executed, the result like this :
I want to make the result like this :
I use conditional aggregation like this :
SELECT i.number, i.description,
MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
attr_maps am
ON am.number = i.number
WHERE i.number = AB123
GROUP BY i.number, i.description
It works
But i'm still confused to add where condition. So I want it can filter by brand etc
I try like this :
SELECT i.number, i.description,
MAX(CASE WHEN am.attribute_code = 'brand' then am.attribute_value END) as brand,
MAX(CASE WHEN am.attribute_code = 'model' then am.attribute_value END) as model,
MAX(CASE WHEN am.attribute_code = 'category' then am.attribute_value END) as category,
MAX(CASE WHEN am.attribute_code = 'subcategory' then am.attribute_value END) as subcategory
FROM items i JOIN
attr_maps am
ON am.number = i.number
WHERE brand = 'honda'
GROUP BY i.number, i.description
There exist error Unknown column 'brand' in 'where clause'
How can I solve the error?
Change the last part of your query
WHERE brand = 'honda'
GROUP BY i.number, i.description
to
GROUP BY i.number, i.description
HAVING brand = 'honda'
I'm querying a MySQL db for a report in SSRS 2008, and I have no problem using the following to join two different queries...
SELECT a.arrivalDate, a.order_item_id, b.roomType
FROM
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a,
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b
WHERE a.order_item_id = b.order_item_id
...but when I try to add a third, it's taking issue with my WHERE clause, but I coulda swore I used to do it this way:
SELECT a.arrivalDate, a.order_item_id, b.roomType, c.retreatDate
FROM
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a,
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b,
(select order_item_id, meta_value as retreatDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'retreat-date') as c,
WHERE a.order_item_id = b.order_item_id AND a.order_item_id = c.order_item_id
Any advice?
Never mind, got it! I thought this is what I tried originally, but I must've had a typo. Hopefully, my error will help others stumbling across this in the future:
SELECT a.arrivalDate, a.order_item_id, b.roomType, c.retreatDate
FROM
(select order_item_id, meta_value as arrivalDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'Arrival Date') as a,
(select order_item_id, meta_value as roomType from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'type-of-room') as b,
(select order_item_id, meta_value as retreatDate from `wp_woocommerce_order_itemmeta` WHERE `meta_key` LIKE 'retreat-date') as c
where a.order_item_id = b.order_item_id AND b.order_item_id = c.order_item_id
I would stick with either of the conventional solutions - the first is (IMO) easier to read. The second is typically faster...
Query 1
SELECT order_item_id
, MAX(CASE WHEN meta_key = 'Arrival Date' THEN meta_value END) arrivalDate
, MAX(CASE WHEN meta_key = 'type-of-room' THEN meta_value END) roomType
, MAX(CASE WHEN meta_key = 'retreat-date' THEN meta_value END) retreatDate
FROM wp_woocommerce_order_itemmeta
GROUP
BY order_item_id
Query 2
SELECT DISTINCT x.order_item_id
, arrival.meta_value arrivalDate
, room.meta_value roomType
, date.meta_value retreatDate
FROM wp_woocommerce_order_itemmeta x
LEFT
JOIN wp_woocommerce_order_itemmeta arrival
ON arrival.order_item_id = x.order_item_id
AND arrival.meta_key = 'Arrival Date'
LEFT
JOIN wp_woocommerce_order_itemmeta room
ON room.order_item_id = x.order_item_id
AND room.meta_key = 'type-of-room'
LEFT
JOIN wp_woocommerce_order_itemmeta date
ON date.order_item_id = x.order_item_id;
I'm not sure how to word what I need so here goes... This is part of a search that allows a type and a state to be selected. I need to be able to run this query to match the type and state from the same mysql column which is meta_value. The problem is it's returning every type for the selected state in the result.
Example: I pick Drag Strip for the type and South Carolina for the state I only want to see the Drag Strip's in South Carolina and not the other types (oval, road course, etc) that are in South Carolina or any other state.
Below is what I have right now that isn't working, I'm not really sure where to go with it to get the result I need.
SELECT dmp_postmeta.meta_value, dmp_posts.post_title, dmp_posts.post_type,
dmp_posts.post_status, dmp_posts.id
FROM dmp_postmeta, dmp_posts
WHERE dmp_postmeta.meta_value IN ('South Carolina','Drag Strip')
AND dmp_posts.post_type='my_custom_tracks'
AND dmp_posts.post_status='publish'
GROUP BY dmp_postmeta.post_id ORDER BY dmp_posts.post_title
meta_key meta_value
Track_Type Drag Strip
Track_Type Oval
Track_Location South Carolina
Track_Location Texas
******Solved******
"SELECT p.ID as id, p.post_title, p.post_type, p.post_status as post_status, MAX(CASE WHEN pm1.meta_value='".$track_type."' then pm1.meta_value ELSE NULL END) as track_type, MAX(CASE WHEN pm1.meta_value='".$track_state."' then pm1.meta_value ELSE NULL END) as track_state FROM dmp_posts p LEFT JOIN dmp_postmeta pm1 ON ( pm1.post_id = p.ID) WHERE p.post_type='my_custom_tracks' AND p.post_status='publish' GROUP BY p.ID, p.post_title ORDER BY p.post_title"
This is what I finally got to work. Thanks for the help guys.
"SELECT p.ID as id, p.post_title, p.post_type, p.post_status as post_status, MAX(CASE WHEN pm1.meta_value='".$track_type."' then pm1.meta_value ELSE NULL END) as track_type, MAX(CASE WHEN pm1.meta_value='".$track_state."' then pm1.meta_value ELSE NULL END) as track_state FROM dmp_posts p LEFT JOIN dmp_postmeta pm1 ON ( pm1.post_id = p.ID) WHERE p.post_type='my_custom_tracks' AND p.post_status='publish' GROUP BY p.ID, p.post_title ORDER BY p.post_title"
I have a table structure as shown below
I am running a query for search that searches for property id with provided
meta_value against meta_name.
I have used union, self join, union all methods but were not working, please suggest me query that will get a data
mysql queries are
1)
SELECT property_id from
real_new_properties
where (meta_name = 'price' and meta_value = '1000')
and (meta_name = 'propertyvalue' and meta_value = '10000')
2)
SELECT property_id
from real_new_properties
where meta_name = 'price'
and meta_value = '1000'
UNION
SELECT property_id
from real_new_properties
where meta_name = 'propertyvalue'
and meta_value = '10000'
3)
SELECT property_id
from real_new_properties
where meta_name = 'price'
and meta_value = '1000'
UNION ALL
SELECT property_id
from real_new_properties
where meta_name = 'propertyvalue'
and meta_value = '10000'
to get all that have meta_name price and propertyvalue and meta_value 1000 and 10000 respectively, you need to do this.
SELECT * FROM `table` WHERE (meta_name = 'price' and meta_value = '1000') OR (meta_name = 'propertyvalue' and meta_value = '10000')
this will select both from table where meta_name = 'price' and meta_value = '1000' and meta_name = 'propertyvalue' and meta_value = '10000'
totally new to MySQL and am trying to extract some data - I was expecting this to be a relatively simple task but I didnt realise that the "format" of the data extract wouldnt have the column headings i expected. I've done some research and think I'm very close to the answer, provided here:
SQL - How to transpose?
However, when writing my query I'm getting a syntax error.
So here is the code I've applied (using a little common sense to work out my specific values, although may have totally missed the mark!)
SELECT wp_usermeta.User_ID
MAX(CASE WHEN wp_usermeta.meta_key = 'nickname' THEN wp_usermeta.meta_value ELSE NULL END) AS 'nickname',
MAX(CASE WHEN wp_usermeta.meta_key = 'sex' THEN wp_usermeta.meta_value ELSE NULL END) AS 'sex'
FROM wp_usermeta
GROUP BY wp_usermeta.User_ID
The error I'm getting is a #1064 advising to check the syntax on line 2 near 'MAX(CASE WHEN wp_usermeta.meta_key = 'nickname' THEN wp_usermeta.meta_value ELSE'
I'm sure this is a simple syntax error but i can't work it out!
Many thanks,
Stuart
In wordpress you can use WPDB class to retrieve results from raw query
global $wpdb;
$results=$wpdb->get_results( "SELECT wp_usermeta.User_ID,
MAX(CASE WHEN wp_usermeta.meta_key = 'nickname' THEN wp_usermeta.meta_value ELSE NULL END) AS 'nickname',
MAX(CASE WHEN wp_usermeta.meta_key = 'sex' THEN wp_usermeta.meta_value ELSE NULL END) AS 'sex'
FROM wp_usermeta
GROUP BY wp_usermeta.User_ID" );
if(!empty($results)){
foreach($results as $r){
echo $r->User_ID ."<br />";
echo $r->nickname ."<br />";
echo $r->sex ."<br />";
}
}
Wordpress Class WPDB
If you write your SELECTs this way, then that problem can never occur...
SELECT m.User_ID
, MAX(CASE WHEN m.meta_key = 'nickname' THEN m.meta_value END) nickname
, MAX(CASE WHEN m.meta_key = 'sex' THEN m.meta_value END) sex
FROM wp_usermeta m
GROUP
BY m.User_ID