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'
Related
I am trying to UPDATE table value where meta key value _stock_status and id value list of ids, but i got an error (You can't specify target table 'wpp' for update in FROM clause). please advise here is my query
UPDATE meta_post AS wpp
SET wpp.meta_value = 'instock'
WHERE wpp.meta_key = '_stock_status'
AND wpp.id IN (
SELECT DISTINCT id
FROM meta_post
WHERE meta_key = '_stock'
AND (meta_value BETWEEN 2 AND 4)
)
thank you
Need to update column value where meta_key = '_stock_status' and id = [1,2,3,4]
I found solution, below query works fine for me.
UPDATE meta_post AS wpp
SET wpp.meta_value = 'instock'
WHERE wpp.meta_key = '_stock_status'
AND wpp.id IN (
SELECT * FROM (
SELECT DISTINCT id FROM meta_post
WHERE meta_key = '_stock'
AND (meta_value BETWEEN 2 AND 4)
) AS pids
)
Thanks for your valuable time.
UPDATE meta_post AS wpp
JOIN (
SELECT id
FROM meta_post
WHERE meta_key = '_stock'
AND meta_value BETWEEN 2 AND 4
) tmp USING (id)
SET wpp.meta_value = 'instock'
WHERE wpp.meta_key = '_stock_status'
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)
I cannot find the meta key for the stock quantity in Woocommerce. What would be the best way to update the stock quantity in Woocommerce using MYSQL?
it's more secure you use the PHP functions. But if you want to update using sql, you can try this:
Checking stock
SELECT * FROM olenka.wp_postmeta where meta_key = '_stock';
Checking stock status:
SELECT post_id AS id, meta_value AS stockstatus FROM wp_postmeta WHERE meta_key = '_stock_status'
Set manage stock true:
UPDATE wp_postmeta SET meta_value = 'yes' WHERE meta_key = '_manage_stock'
Set the 'instock' flag:
UPDATE wp_postmeta set meta_value = 'instock' WHERE meta_key = '_stock_status' AND meta_value = 'outofstock'
Update the stock
UPDATE wp_postmeta SET meta_value = 99 WHERE meta_key = '_stock' AND meta_value IS NULL;
OR
UPDATE wp_postmeta SET meta_value = 99 WHERE meta_key = '_stock' AND meta_value > 0;
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 have a SQL statement here I need rows that are within a certain column to be divided into rows... but when I retrieve the data is returns the error
1 Subquery returns more than 1 row
:( please help here is my query
(select
(SELECT
(meta_value)
FROM
main_postmeta
WHERE
meta_key = 'project_year') as Year,
(SELECT
(meta_value)
FROM
main_postmeta
WHERE
meta_key = 'project_gapp_city') as Location,
(SELECT
(meta_value)
FROM
main_postmeta
WHERE
meta_key = 'project_title') as Title
FROM
main_postmeta
WHERE
main_postmeta.post_id = '1423'
ORDER BY meta_value desc);
You seem to be looking for a pivoted result set.
select
case when meta_key='project_year' then meta_value else null end as `YEAR`
, case when meta_key='project_gapp_city' then meta_value else null end as `LOCATION`
, case when meta_key='project_title' then meta_value else null end as `Title`
from main_postmeta
where main_postmeta.post_id = '1423'
order by meta_value desc
If the values fetching are grouped under a single post_id then:
select
max ( case when meta_key='project_year'
then meta_value
else null
end ) as `YEAR`
, max ( case when meta_key='project_gapp_city'
then meta_value
else null
end ) as `LOCATION`
, max ( case when meta_key='project_title'
then meta_value
else null
end ) as `Title`
from main_postmeta
where main_postmeta.post_id = '1423'
order by meta_value desc
Probably best to do a join for each meta value, something like this:-
SELECT a.meta_value, b.meta_value, c.meta_value
FROM main_postmeta a
INNER JOIN main_postmeta b ON a.post_id = b.meta_key AND b.meta_key = 'project_gapp_city'
INNER JOIN main_postmeta c ON a.post_id = c.meta_key AND c.meta_key = 'project_title'
WHERE a.post_id = '1423'
AND a.meta_key = 'project_year'
ORDER BY meta_value DESC
Depending on what you want to do with the results you might also be able to do something like this:-
SELECT GROUP_CONCAT(CONCAT_WS('|', meta_key, meta_value))
FROM main_postmeta a
WHERE a.post_id = '1423'
AND a.meta_key IN ( 'project_gapp_city', 'project_year', 'project_title')
and then explode out the keys and values.