I need to show the contents of two tables; my query is this:
SELECT ID,
post_author,
post_content,
post_title,
post_name,
guid,
post_type,
meta_id,
post_id,
meta_key,
meta_value
FROM wp_posts,
wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND post_author IN (2);
The meta_key column stores the TYPE of content I need like website, latitude, longitude, phone, email. Is there a way to make this column explode to another columns? For example:
ID,
post_author,
...,
meta_id,
post_id,
meta_key,
meta_value,
website(its a meta_key result),
latitude(another meta_key result),
longitude(anoter meta_key result)...
It is not clear what are you trying to do, but I think you need to display the meta key and values as columns' headers with the meta values as the values for each column. In this case you can do something like this:
SELECT
ID,
post_author,
post_content,
post_title,
post_name,
guid,
post_type,
meta_id,
post_id,
MAX(CASE WHEN meta_key = 'website' THEN meta_value END) AS website,
MAX(CASE WHEN meta_key = 'latitude' THEN meta_value END) AS latitude,
MAX(CASE WHEN meta_key = 'longitude' THEN meta_value END) AS longitude,
...
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE post_author IN (2)
GROUP BY Id, post_author, ...
Things to note:
Try to avoid the old JOIN syntax that you used in your query; it is a bad habit , and use the ANSI-SQL-92 syntax using the JOIN key word instead like what I did.
I used the MAX as a work around to pivot the rows into columns, you will need to list the columns that are not in the aggregate function in the GROUP BY clause.
Related
I'm trying to pull some data out of the wp_postmeta table which is basically a series of key/value pairs tied to a numeric post_id. As such, when I try to extract various values for a post, this is what I get:
This is the current query I'm using to get that output:
select post_id,meta_key,meta_value from wp_postmeta
where meta_key in ('_sku','_length','_width','_height')
and post_id in (
select post_id from wp_postmeta
where meta_value in ('28-005080','28-005287')
)
order by post_id DESC
What I'm trying to do is format the information like this:
I've tried to look at the MySql pivot table examples, but I'm not sure if they quite fit this specific scenario. Frankly, I don't know where to start with accomplishing this task.
You can use conditional aggregation:
select post_id,
max(case when meta_key = '_sku' then meta_value end) as sku,
max(case when meta_key = '_length' then meta_value end) as length,
max(case when meta_key = '_width' then meta_value end) as width,
max(case when meta_key = '_height' then meta_value end) as height
from wp_postmeta
where
meta_key in ('_sku','_length','_width','_height')
and post_id in (select post_id from wp_postmeta where meta_value in ('28-005080','28-005287'))
group by post_id
order by post_id desc
Actually, we might be able to replace the subquery in the where clause with a having clause:
select post_id,
max(case when meta_key = '_sku' then meta_value end) as sku,
max(case when meta_key = '_length' then meta_value end) as length,
max(case when meta_key = '_width' then meta_value end) as width,
max(case when meta_key = '_height' then meta_value end) as height
from wp_postmeta
group by post_id
having max(meta_value in ('28-005080','28-005287')) = 1
order by post_id desc
I want add to wp_postmeta meta_key record if this doesn't exists.
I had this query, but from something strange reason it doesn't work anymore. Please help!
INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT '_update_date', '', ID FROM wp_posts WHERE wp_posts.post_type='product'
AND NOT EXISTS (SELECT meta_key FROM wp_postmeta WHERE meta_key = '_update_date' );
If you wanted to insert into wp_postmeta IF
SELECT '_update_date', '', ID FROM wp_posts WHERE wp_posts.post_type='product'
Doesn't EXISTS.....
I think this query will work . You were confused with syntax and missed where
`INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT meta_key FROM wp_postmeta WHERE meta_key = '_update_date'
WHERE NOT EXISTS(SELECT '_update_date', '', ID FROM wp_posts WHERE
wp_posts.post_type='product')`
Hope this helps.
I'm trying to make a mysql query which select all the posts from wp_posts and select the post_title and post_content from it and then select the table wp_postmeta and show the meta_value where meta_key is equal with "_wp_attached_file".
I've tried doing this:
SELECT post_title, post_content, meta_value FROM wp_posts, wp_postmeta WHERE ID = post_id and meta_key = '_wp_attached_file'
But this will only show if there is a meta_key equal to '_wp_attached_file'.
The query should show all posts and if it has a meta_key equal to '_wp_attached_file'. Then show meta_value else just show nothing or NULL?
How can i do this?
Use LEFT JOIN
SELECT post_title, post_content, meta_value
FROM wp_posts
LEFT JOIN
wp_postmeta ON ID = post_id
and meta_key = '_wp_attached_file'
i am using below insert query adding custom filed for all wordpress post, but getting Sql Syntax error i dont know where i m doing wrong
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'CustomField'
AS meta_key 'MyValue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'CustomField')
'' AND post_type = 'post';
you missed comma for separation of columns and single quote to wrap string.
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id,
'CustomField' AS meta_key,
'MyValue' AS meta_value
FROM wp_posts
WHERE ID NOT IN
(SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'CustomField')
AND post_type = 'post';
An alternative of using NOT IN is by joining two tables,
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT a.ID AS post_id,
'CustomField' AS meta_key,
'MyValue' AS meta_value
FROM wp_posts a
LEFT JOIN wp_postmeta b
ON a.ID = b.post_id AND
b.meta_key = 'CustomField'
WHERE a.post_type = 'post' AND
b.post_id IS NULL
I have a table wp_postmeta with columns called meta_key and meta_value. I have 5 records in meta_key (location,area,price,bedrooms,bathrooms)
For example, I want to find a hotel in Texas with 2 bathrooms:
select post_id from wp_postmeta where meta_key = 'location' and meta_value = 'texas' and where meta_key = 'bathrooms' and meta_value= '2';
I know the above SQL command is not valid. Can anyone please help me to achieve the above result?
You can try mysql subquery:
select post_id
from wp_postmeta
where meta_key = 'location' and meta_value = 'texas'
and post_id IN (select post_id
from wp_postmeta
where meta_key = 'bathrooms' and meta_value= '2')