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')
Related
I need get value if two rows have same value. I need get post_id one field 145 in output.
Should i use JOIN or not? Or maybe some simpler/faster way to do this?
But this is not correct one...But i need someting like this, when barcode and _sku values are SAME.
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'barcode' = '_sku' AND meta_value = '123';
Getting error and checking just barcode field...
Warning: #1292 Truncated incorrect DECIMAL value: '_sku'
Since MySQL 8.0 you can use INTERSECT:
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'barcode' AND meta_value = '123'
INTERSECT
SELECT post_id
FROM wp_postmeta
WHERE meta_key = '_sku' AND meta_value = '123';
https://sqlize.online/sql/mysql80/d0a7123a10d11065d9f0609044c70d35/
Another way is use GROUP BY and HAVING:
SELECT post_id
FROM wp_postmeta
WHERE meta_key IN ('barcode', '_sku') AND meta_value = '123'
GROUP BY post_id
HAVING COUNT(DISTINCT meta_key) = 2;
https://sqlize.online/sql/mysql80/594f4b262c55c10d4958f3bc54c7353b/
SELECT post_id
FROM wp_postmeta
WHERE
meta_key IN ('barcode')
AND meta_value = '123'
AND EXISTS (SELECT post_id FROM wp_postmeta pm WHERE meta_key IN ('_sku')
AND meta_value = '123' AND pm.post_id = wp_postmeta.post_id)
;
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