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;
Related
I've developed a query which selects events from Wordpress. I am using the where clause to select where the meta_value of the meta_key eventstartdate is after today.
The issue I'm having now is, I also want filter on a second meta_value that being from the meta_key '_VenueCity'.
I have tried aliasing the wp_postmeta table and doing a where on the meta_key but I think I'm missing a join.
This is the code that works without my additional code to get it to work. Can any one advise on how I get this to work?
SELECT
`wp_posts`.`ID` AS `EventID`,
`wp_posts`.`post_parent` AS `SeriesID`,
`wp_posts`.`post_title` AS `EventTitle`,
`wp_posts`.`post_content` AS `EventDescription`,
`wp_posts`.`post_excerpt` AS `EventSummary`,
`wp_posts`.`post_name` AS `EventSlug`,
min(`wp_postmeta`.`meta_value`) AS `EventStartDate`,
max(`tribe_event_end_date`.`meta_value`) AS `EventEndDate`,
`wp_posts`.`guid` AS `GUID`
FROM ((`wp_posts`
JOIN `wp_postmeta` ON
(
(`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
))
LEFT JOIN `wp_postmeta` `tribe_event_end_date` ON
(
(
(`wp_posts`.`ID` = `tribe_event_end_date`.`post_id`) AND
(`tribe_event_end_date`.`meta_key` = '_EventEndDate')
)
))
WHERE
(
(`wp_postmeta`.`meta_key` = '_EventStartDate') AND
(`wp_posts`.`post_type` = 'tribe_events') AND
(`wp_posts`.`post_status` = 'publish') AND
(`tribe_event_end_date`.`meta_value` >= CURDATE())
)
GROUP BY
`wp_posts`.`ID`
ORDER BY
`EventStartDate`,
`wp_posts`.`post_date`;
I am not going to write your query for you but I will give an example of how to get multiple postmeta values. The power will be in the where clause to get the right values.
You should also consider what joins you want to use.
SELECT
p.post_title,
pm1.meta_value,
pm2.meta_value
FROM wp_posts as p
INNER JOIN wp_postmeta as pm1
ON p.ID = pm1.post_id
INNER JOIN wp_postmeta as pm2
ON p.ID = pm2.post_id
WHERE
pm1.meta_key = '_my_postmeta_field1'
AND
pm2.meta_key <> '_not_this_field'
I am looking for a solution for:
SELECT ID
, post_date_gmt
, post_content
, post_title
, post_modified_gmt
FROM wphj_posts
WHERE post_status = 'publish'
and:
SELECT DISTINCT meta_value
, post_id
, meta_id
FROM wphj_postmeta
WHERE meta_key LIKE '_wp_attached_file'
ORDER
BY meta_value ASC
What I did first was this, which doesn't work as I want it to:
SELECT ID
, post_date_gmt
, post_content
, post_title
, post_modified_gmt
, meta_value
FROM wphj_posts
, wphj_postmeta
WHERE post_status = 'publish'
AND meta_key LIKE '_wp_attached_file'
Am simply trying to query WordPress database to get post data from wp_posts and the featured image of the same post from the wp_postmeta table.
Stuff I tried to do: used union, unionall, join on, inner join, left join. Unfortunately they don't work.
Update, came up with this using wpDatatables plugin, works on the plugin dashboard but not as a query on its own :\ - what am i doing wrong folks
SELECT posts_post.ID AS post_ID,
posts_post.post_date_gmt AS post_post_date_gmt,
posts_post.post_title AS post_post_title,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post,
posts_post.post_content AS post_post_content,
posts_post.post_status AS post_post_status,
posts_post.post_name AS post_post_name,
CONCAT(
'<a href="',
posts_post.guid,
'"><img src="',
REPLACE(
posts_post_img.guid,
CONCAT(
'.',
SUBSTRING_INDEX(
posts_post_img.guid,
'.',
-1
)
),
CONCAT(
'-150x150.' ,
SUBSTRING_INDEX(
posts_post_img.guid,
'.',
-1
)
)
),
'" /></a>'
) AS post_thumbnail_with_link_to_post,
post_taxonomy_category_tbl.name AS post_taxonomy_category
FROM wphj_posts AS posts_post
INNER JOIN (SELECT name, object_id as id FROM wphj_terms AS post_taxonomy_category_tbl_terms INNER JOIN wphj_term_taxonomy AS post_taxonomy_category_tbl_termtaxonomy ON post_taxonomy_category_tbl_termtaxonomy.term_id = post_taxonomy_category_tbl_terms.term_id AND post_taxonomy_category_tbl_termtaxonomy.taxonomy = 'category' INNER JOIN wphj_term_relationships AS rel_post_taxonomy_category_tbl ON post_taxonomy_category_tbl_termtaxonomy.term_taxonomy_id = rel_post_taxonomy_category_tbl.term_taxonomy_id) AS post_taxonomy_category_tbl
ON post_taxonomy_category_tbl.ID = posts_post.id
LEFT JOIN (SELECT posts_post_imgposts.guid AS guid, posts_post_imgpostmeta.post_id AS post_id
FROM wphj_postmeta AS posts_post_imgpostmeta
INNER JOIN wphj_posts AS posts_post_imgposts
ON posts_post_imgpostmeta.meta_value = posts_post_imgposts.ID
WHERE posts_post_imgpostmeta.meta_key = '_thumbnail_id' ) AS posts_post_img
ON posts_post_img.post_id = posts_post.ID
WHERE 1=1
AND posts_post.post_type = 'post'
You can try this code, I ran it in sequal pro on on of my local installations, and I got the it to work as I think you would like it to work.
SELECT ID,
post_date_gmt,
post_content,
post_title,
post_modified_gmt,
meta_value
FROM wphj_posts, wphj_postmeta
WHERE wphj_posts.ID = wphj_postmeta.post_id
AND wphj_postmeta.meta_key = '_wp_attached_file'
AND wphj_posts.post_status = 'publish'
I am trying to construct a select statement. The table structure is shown below. I am basically looking to select the three fields on the left table along with the meta_values of the right hand side table that have a meta_key of responsible_service and responsible_officer. These meta_values may or may not exist.
My awful attempt looks like this.
SELECT
`wp_posts`.`ID`,
`wp_posts`.`post_content`,
`wp_posts`.`post_title`
FROM(
`wp_posts`
INNER JOIN `wp_postmeta` ON (`wp_posts`.`ID` = `wp_postmeta`.`post_id`),
(Select `wp_postmeta`.meta_value where`wp_postmeta`.meta_key='responsible_officer') as Responsible Officer),
(Select `wp_postmeta`.meta_value where `wp_postmeta`.meta_key='responsible_service') as Responsible Service ),
The result should look like this
This will give you 1 or more rows per wp_posts.ID depending on how many 'responsible_officer' and 'responsible_service' are found. If none are found meta_key and meta_value will be null
SELECT
`wp_posts`.`ID`,
`wp_posts`.`post_content`,
`wp_posts`.`post_title`,
max(case `wp_postmeta`.meta_key
when 'responsible_officer' then `wp_postmeta`.meta_value end) as responsible_officer,
max(case `wp_postmeta`.meta_key
when 'responsible_service' then `wp_postmeta`.meta_value end) as responsible_service
FROM
`wp_posts`
LEFT JOIN `wp_postmeta` ON `wp_posts`.`ID` = `wp_postmeta`.`post_id`
AND `wp_postmeta`.meta_key in ('responsible_officer', 'responsible_service')
group by `wp_posts`.`ID`,
`wp_posts`.`post_content`,
`wp_posts`.`post_title`;
Check this also:
select postmeta.meta_value, posts.ID, posts.`post_content`,
posts.`post_title` from `wp_posts` posts join `wp_postmeta` postmeta on posts.ID = postmeta.post_id where postmeta.meta_key IN ('responsible_officer', 'responsible_service')
SELECT
`wp_posts`.`ID`,
`wp_posts`.`post_content`,
`wp_posts`.`post_title`
FROM `wp_posts`
JOIN `wp_postmeta` ON (`wp_posts`.`ID` = `wp_postmeta`.`post_id`)
where `wp_postmeta`.meta_key in ('responsible_officer', 'responsible_service')
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'
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.