Inner JOIN missing row - mysql

First I would like to mention I have limited skills when it comes to MYSQL an JOIN. However this is what I have and what I like to achieve:
I have the default WordPress tables and like to get a result with post_name, title, status and the meta_value from a certain meta key.
This is what I have:
SELECT
wp_posts.ID, wp_posts.post_name, wp_posts.post_title, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN
wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE (
wp_term_relationships.term_taxonomy_id
IN ( 1, 2, 3 )
)
AND wp_posts.post_type = 'my_post_type'
AND (
wp_posts.post_status
IN (
'my_status_1', 'my_status_2'
)
)
AND wp_postmeta.meta_key = 'my_meta_key'
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
Everything works as expected when each post has a postmeta of 'my_meta_key'. But if the 'my_meta_key' is missing the post is not in the result.
I guess it' caused by the second INNER JOIN but as mentioned I have no idea what I should it replace it with.
I'm sure it's something simple

Move the AND wp_postmeta.meta_key = 'my_meta_key' to
INNER JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
Like this and change the INNER for a LEFT
LEFT JOIN
wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'my_meta_key')
If you put your condition in the "Where Clause" The Left join will be "overwritten".
And I will add an advice. Dont use parenthesis when you dont really need it to keep your code easy to read.

The following returns
all records from WP_Posts
only those records with WP_TERM_RELATIONSHIPS
only those records in wp_postmeta which a matching record in wp_posts.
*
SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title,
wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
LEFT JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_term_relationships.term_taxonomy_id IN ( 1, 2, 3 )
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_statusIN ('my_status_1', 'my_status_2')
AND (wp_postmeta.meta_key = 'my_meta_key' or wp_postmeta.meta_key is null)
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
you need the is null otherwise records in wp_posts without wp_postmeta data will be excluded.

Change the INNER JOIN on the wp_postmeta table to a LEFT JOIN:
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
And move the WHERE filter for the wp_postmeta to the JOIN condition.
So your query will be:
SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_term_relationships
ON ( wp_posts.ID = wp_term_relationships.object_id )
LEFT JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
AND wp_postmeta.meta_key = 'my_meta_key'
WHERE wp_term_relationships.term_taxonomy_id IN ( 1, 2, 3 )
AND wp_posts.post_type = 'my_post_type'
AND wp_posts.post_status IN ('my_status_1', 'my_status_2')
GROUP BY wp_posts.ID
ORDER BY wp_posts.ID ASC
The INNER JOIN syntax will only return rows that match in both tables. So if you do not have a matching row, you will not get any result. By changing that to a LEFT JOIN, you will return all rows even if there is not a matching row in the wp_postmeta table. If the row does not exist, then the values from the wp_postmeta table will be null.

Related

mysql DELETE and limit

I have a query that is working to display rows who contain specific meta key.
select wp_woocommerce_order_itemmeta.*
from wp_posts, wp_postmeta
inner join wp_woocommerce_order_items, wp_woocommerce_order_itemmeta
where wp_posts.post_type = "shop_subscription"
and wp_postmeta.post_id = wp_posts.ID
and wp_postmeta.meta_key = "_shipping_country"
and wp_postmeta.meta_value = "FR"
and wp_woocommerce_order_items.order_id = wp_posts.ID
and wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
and wp_woocommerce_order_itemmeta.meta_key = "_subtracted_base_location_taxes"
I want to delete these rows but with a limit. I ve got an error with this sql
DELETE wp_woocommerce_order_itemmeta.*
FROM wp_posts, wp_postmeta
inner join wp_woocommerce_order_items, wp_woocommerce_order_itemmeta
WHERE wp_posts.post_type = "shop_subscription"
and wp_postmeta.post_id = wp_posts.ID
and wp_postmeta.meta_key = "_shipping_country"
and wp_postmeta.meta_value = "FR"
and wp_woocommerce_order_items.order_id = wp_posts.ID
and wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
and wp_woocommerce_order_itemmeta.meta_key = "_subtracted_base_location_taxes"
LIMIT 100
I have read that you can't use LIMIT directly within DELETE when you're referencing multiple tables at the same time. I begin with mysql and i am stuck. Any help would be apreciate.
Thanks
try something like this
delete a
from wp_woocommerce_order_itemmeta a
join (
select wp_woocommerce_order_itemmeta.meta_id
from wp_posts, wp_postmeta
inner join wp_woocommerce_order_items, wp_woocommerce_order_itemmeta
where wp_posts.post_type = 'shop_subscription'
and wp_postmeta.post_id = wp_posts.ID
and wp_postmeta.meta_key = '_shipping_country'
and wp_postmeta.meta_value = 'FR'
and wp_woocommerce_order_items.order_id = wp_posts.ID
and wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
and wp_woocommerce_order_itemmeta.meta_key = '_subtracted_base_location_taxes'
limit 100
) b on a.meta_id = b.meta_id
mysql delete with inner joins and limit

Display posts from specific category in wordpress

With this query i get the posts i need from my wordpress db. What i have to do to get the posts from a specific category?
$sql = "select DISTINCT wp_posts.id, wp_posts.post_title, wp_posts.post_excerpt, wp_posts.guid, wp_posts.post_type, featured_image.guid as post_image, wp_posts.post_modified, wp_users.display_name
from wp_posts
inner join wp_postmeta on wp_posts.id = wp_postmeta.post_id and wp_postmeta.meta_key = '_thumbnail_id'
inner join wp_posts as featured_image on featured_image.id = wp_postmeta.meta_value
inner join wp_users on wp_users.id = wp_posts.post_author
where wp_posts.post_status = 'publish'
AND DATE_FORMAT(wp_posts.post_date, '%d-%m-%Y')='$mydate'
order by wp_posts.ID desc
limit 20";
You have to have joins with three more tables: wp_term_relationships, wp_term_taxonomy and wp_terms, and in the where clause you can search for the category name from the name column of the terms table. Have updated the sql query
$sql = "select DISTINCT wp_posts.id, wp_posts.post_title, wp_posts.post_excerpt, wp_posts.guid, wp_posts.post_type, featured_image.guid as post_image, wp_posts.post_modified, wp_users.display_name
from wp_posts
inner join wp_postmeta on wp_posts.id = wp_postmeta.post_id and wp_postmeta.meta_key = '_thumbnail_id'
inner join wp_posts as featured_image on featured_image.id = wp_postmeta.meta_value
inner join wp_users on wp_users.id = wp_posts.post_author
INNER JOIN wp_term_relationships rel ON wp_posts.ID = rel.object_id
INNER JOIN wp_term_taxonomy taxonomy ON rel.term_taxonomy_id = taxonomy.term_taxonomy_id
INNER JOIN wp_terms terms ON taxonomy.term_id = terms.term_id
where wp_posts.post_status = 'publish'
AND terms.name = 'Category you wist to search for'
AND DATE_FORMAT(wp_posts.post_date, '%d-%m-%Y')='$mydate'
order by wp_posts.ID desc
limit 20";

DELETE rows from two tables which do not have an id match to another query of the same two tables

The following does not necessarily use correct syntax, but is intended to convey my intention:
DELETE FROM wp_posts, wp_postmeta
WHERE wp_posts.ID <> min(f.post_ID), wp_postmeta.post_id <> min(f.post_ID)
(It's really two deletes combined in one, with the WHERE condition for the first and second table respectively.)
where min(f.post_ID) comes from the virtual table below:
SELECT Min(f.post_id),
f.post_title,
f.meta_value
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
How would the query look?
Edit: It's worth considering that any implementation that deletes from one table before the other will change the outcome of the second delete. (The second DELETE will be affected by the first delete because items from the table in the sub query were deleted in the first DELETE.)
You don't delete from two tables at the same time. If the tables are related, you delete from the child, then the parent. If they're unrelated the delete may happen in any order. If they're unrelated but have other tables depending on them (i.e. they are parents themselves and have children) then data must be cleared out of those other tables first. If the relational constraints are set to CASCADE DELETE mode, then child table data will be deleted automatically when parent table data is deleted. If the delete must happen as an all or nothing affair (i.e. if the second delete fails after the first delete succeeds you don't want the first delete to succeed), it should be done in a transaction.
Thus:
DELETE FROM wp_postmeta WHERE post_id NOT IN (
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
)
DELETE FROM wp_posts WHERE ID NOT IN (
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
)
Warning
Don't run a delete query given to you by someone on the internet without backing your data up first. At the very least, start a transaction, run the delete, select the results and look at them to ensure they're correct, using the following code pattern:
START TRANSACTION;
DELETE FROM ...
DELETE FROM ...
SELECT * FROM ... -/*to check the deletes worked and didn't remove too much*/
ROLLBACK;
Change the ROLLBACK to COMMIT when you're happy
Edit:
Option 1
Make the posts meta depend on posts (it may do already, ensure the deletes are cascaded):
ALTER TABLE posts_meta
ADD CONSTRAINT fk_pm FOREIGN KEY (posts_id) REFERENCES posts(id) ON DELETE CASCADE
Now run your delete on the posts table, posts_meta entries will also disappear
Option2
Run your delete on posts table as recommended above
Use the following query to delete any record from posts_meta that doesnt have a matching record in posts:
DELETE FROM posts_meta WHERE post_id IN (select post_id from(
SELECT pm.post_id
FROM
posts_meta pm
LEFT JOIN
posts p
ON p.id = pm.post_id
WHERE
p.id IS NULL
) i )
The innermost subquery that finds the id list is wrapped inside another subquery for a reason; there are situations where MySQL will refuse a delete if the pattern is DELETE FROM x WHERE y IN (SELECT x FROM y) because you can't modify a table you're selecting from. Wrapping it up in another select is a hack that causes MySQL to not treat it as deleting from the same table you're selecting from
The idea is to select IDs to be deleted from tables wp_posts and wp_postmeta into temporary tables posts and postmeta. This will preserve a list of IDs even if you delete data from any table. Then delete data from tables wp_posts and wp_postmeta one after another based on the list of IDs in the temp tables. The last step is to clear temporary tables (delete data from them).
The transaction is used in that way to allow run this code in PHPMyAdmin.
The optimal version of the code:
BEGIN;
CREATE TEMPORARY TABLE IF NOT EXISTS minPostIds AS
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price') AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title;
DELETE FROM wp_posts WHERE wp_posts.ID IN (SELECT ID FROM minPostIds);
DELETE FROM wp_postmeta WHERE wp_postmeta.post_id IN (SELECT ID FROM minPostIds);
DELETE FROM minPostIds;
COMMIT;
This version works:
BEGIN;
CREATE TEMPORARY TABLE IF NOT EXISTS posts AS
SELECT ID
FROM wp_posts
WHERE wp_posts.ID NOT IN
(
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
);
CREATE TEMPORARY TABLE IF NOT EXISTS postmeta AS
SELECT post_id
FROM wp_postmeta
WHERE wp_postmeta.post_id NOT IN
(
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
);
DELETE FROM wp_posts WHERE wp_posts.ID IN (SELECT ID FROM posts);
DELETE FROM wp_postmeta WHERE wp_postmeta.post_id IN (SELECT post_id FROM postmeta);
DELETE FROM posts;
DELETE FROM postmeta;
COMMIT;
You can also create a stored procedure:
CREATE PROCEDURE DeleteFromTables()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS posts
ENGINE=MyISAM
AS (
SELECT ID
FROM wp_posts
WHERE wp_posts.ID NOT IN
(
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
)
);
CREATE TEMPORARY TABLE IF NOT EXISTS postmeta
ENGINE=MyISAM
AS (
SELECT post_id
FROM wp_postmeta
WHERE wp_postmeta.post_id NOT IN
(
SELECT Min(f.post_id)
FROM (SELECT wp_posts.post_title,
Min(wp_postmeta.meta_value) AS minprice
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
GROUP BY wp_posts.post_title) AS x
INNER JOIN (SELECT wp_postmeta.post_id,
wp_posts.post_title,
wp_postmeta.meta_value
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
ORDER BY wp_posts.post_title,
wp_postmeta.meta_value) AS f
ON x.post_title = f.post_title
AND f.meta_value = x.minprice
GROUP BY f.post_title
)
);
START TRANSACTION;
DELETE FROM wp_posts
WHERE wp_posts.ID IN (SELECT ID FROM posts);
DELETE FROM wp_postmeta
WHERE wp_postmeta.post_id IN (SELECT post_id FROM postmeta);
COMMIT;
DELETE FROM posts;
DELETE FROM postmeta;
END;

How to use mysql to update woocommerce products images

i wanna update thumbnails of all products in specific categories of my shop with one image.
I know the categories id and their term_taxonomy_id but I cannot do proper update statement with subquery ...
Here is my code..
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
AND
wp_postmeta.posts_id = (
SELECT wp_posts.* FROM wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE post_type = 'product' AND taxonomy = 'product_cat'
AND wp_term_taxonomy.term_taxonomy_id = '130')
Where I make mistake ??
product category id 114 and in term_taxonomy_id 130
This part of the question works good but changes ALL products thumbanils not only from specific category
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
Maybe I will ask for help not for resolving my problem in my way :)
This query gives some records
SELECT wp_posts.* FROM wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id
WHERE post_type = 'product' AND taxonomy = 'product_cat'
AND wp_term_taxonomy.term_taxonomy_id = '130'
and I want to use ID (from results of the query above) as wp_postmeta.post_id to change some values in those records in wp_postmeta in the query below
UPDATE wp_postmeta
SET wp_postmeta.meta_value = '5898'
WHERE wp_postmeta.meta_key = '_thumbnail_id'
Anyone ?? Help plis....

Cannot query multiple values on an Inner Join

I'm using this sql to return results based on an inner join with 3 meta values. It only seems to work with 1 AND ( ), when i add the other two it returns 0 results.
SELECT * FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND ( wp_postmeta.meta_key = 'plot_type' AND wp_postmeta.meta_value = 'Cottage' )
AND ( wp_postmeta.meta_key = 'number_of_bedrooms' AND wp_postmeta.meta_value = '2' )
AND ( wp_postmeta.meta_key = 'property' AND wp_postmeta.meta_value = '446' )
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
I think you meant to use OR with the other 2 (see below). The same field can't be 2 different things, which is why you get 0 results.
SELECT *
FROM wp_posts
INNER JOIN wp_postmeta
ON (wp_posts.ID = wp_postmeta.post_id)
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND ((wp_postmeta.meta_key = 'plot_type' AND
wp_postmeta.meta_value = 'Cottage') OR
(wp_postmeta.meta_key = 'number_of_bedrooms' AND
wp_postmeta.meta_value = '2') OR (wp_postmeta.meta_key = 'property' AND
wp_postmeta.meta_value = '446'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
edit, try below instead:
select * from wp_posts
join wp_postmeta on wp_posts.ID = wp_postmeta.post_id
where wp_posts.post_type = 'plot'
and wp_posts.post_status = 'publish'
and concat(wp_postmeta.meta_key,'|',wp_postmeta.meta_value)
in ('plot_type|Cottage',
'number_of_bedrooms|2',
'property|446');
You need to join the wp_postmeta table once for each type of value you need.
SELECT whatever, whatever
FROM wp_posts AS p
JOIN wp_postmeta AS plottype
ON (p.ID = plottype.post_id AND plottype.meta_key = 'plot_type')
JOIN wp_postmeta AS bedrooms
ON (p.ID = bedrooms.post_id AND bedrooms.meta_key = 'number_of_bedrooms')
JOIN wp_postmeta AS property
ON (p.ID = property.post_id AND property.meta_key = 'property')
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND plottype.meta_value = 'Cottage'
AND bedrooms.meta_value = '2'
AND property.meta_value = '466'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
This wp_postmeta key/value storage is a little tricky to join to; your join criteria need to pull the appropriate key as well as the matching post ID.
It's well known that SELECT * is a bad idea in software. It's especially bad when you're joining so many tables. List the columns you need in your result set.
Notice also that you're using INNER JOIN with which JOIN is synonymous. If any of the values you're pulling from the metadata are missing, so will be the row from your result set. You may or may not be better off using LEFT JOINs (You didn't explain the purpose of the query.)
It seems as some of the structure for your conditions should be changed.
Try the following:
SELECT * FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND (
(wp_postmeta.meta_key = 'plot_type' AND wp_postmeta.meta_value = 'Cottage')
OR
(wp_postmeta.meta_key = 'number_of_bedrooms' AND wp_postmeta.meta_value = '2')
OR
(wp_postmeta.meta_key = 'property' AND wp_postmeta.meta_value = '446')
)
ORDER BY wp_posts.post_title ASC;
I managed to fix the issue using WP_Meta_Query, the SQL it produced was...
SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
WHERE 1=1
AND wp_posts.post_type = 'plot'
AND (wp_posts.post_status = 'publish')
AND (
(wp_postmeta.meta_key = 'property' AND CAST(wp_postmeta.meta_value AS CHAR) = '180')
AND (mt1.meta_key = 'plot_type' AND CAST(mt1.meta_value AS CHAR) = 'Cottage')
AND (mt2.meta_key = 'number_of_bedrooms' AND CAST(mt2.meta_value AS CHAR) = '2')
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;
Thanks to everyone for the help :)