Cannot query multiple values on an Inner Join - mysql

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 :)

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

Deleting an ID from the wp_post table

I use this SQL query to get me all the posts that don't have a photo, it shows me 2010 posts, however with what SQL command I can delete those 2010 posts
SELECT *
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = '_thumbnail_id'
WHERE 1 = 1 AND wp_postmeta.post_id IS NULL
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date
One option would be using EXISTS :
DELETE FROM wp_posts
WHERE EXISTS
(
SELECT 1
FROM wp_posts wp1
LEFT JOIN wp_postmeta wp2 ON wp1.ID = wp2.post_id AND wp2.meta_key = '_thumbnail_id'
WHERE wp2.post_id IS NULL
AND wp1.post_type = 'post'
AND (wp1.post_status = 'publish' OR wp1.post_status = 'private')
AND wp1.ID = wp_posts.ID
GROUP BY wp1.ID
)
or an INNER JOIN :
DELETE w
FROM wp_posts w JOIN
(
SELECT wp1.ID
FROM wp_posts wp1
LEFT JOIN wp_postmeta wp2 ON wp1.ID = wp2.post_id AND wp2.meta_key = '_thumbnail_id'
WHERE wp2.post_id IS NULL
AND wp1.post_type = 'post'
AND (wp1.post_status = 'publish' OR wp1.post_status = 'private')
GROUP BY wp1.ID
) w2
ON w.ID = w2.ID

Ordering results of MySQL query (using join)

In a WordPress installation, I need to order products so that:
Sold products show up last.
Sold products tagged "antique" show up after sold products tagged "reproduction."
I have successfully completed the first item, but I am at a loss regarding the second item. I'm not getting any errors. My problem is that everything in my ORDER BY statement is working except for "wt.slug DESC." If I change the LEFT JOIN statements for wp_term_taxonomy and wp_terms tables to INNER JOIN statements, I get 0 results, so it looks to me like these statements are not finding the results that I expect. Here is my query:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
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 )
LEFT JOIN wp_postmeta stock ON ( wp_posts.ID = stock.post_id AND stock.meta_key = '_stock_status' )
LEFT JOIN wp_term_taxonomy wtt ON ( wp_term_relationships.term_taxonomy_id = wtt.term_taxonomy_id AND wtt.taxonomy = 'product_tag' )
LEFT JOIN wp_terms wt ON ( wtt.term_id = wt.term_id AND ( wt.slug = 'antique' OR wt.slug = 'reproduction' ) )
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (171) )
AND ( ( wp_postmeta.meta_key = '_visibility' AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog') ) )
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_type DESC, stock.meta_value ASC, wt.slug DESC, wp_posts.post_date
Here is the WordPress database description for reference. I would appreciate any assistance.
You can create a new field as
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID, CASE wt.slug WHEN 'antique' THEN -1 WHEN 'reproduction' THEN 0 ELSE 1 END as sort_order
and use the sort_order in ORDER BY clause like
ORDER BY sort_order DESC
Thanks to help from #mynawaz, I have been able to come up with a solution. I'm not sure if it's the most efficient or elegant solution, but it works:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* , CASE wt.slug WHEN 'antique' THEN 1 WHEN 'reproduction' THEN 2 ELSE 0 END as slug_order
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 )
LEFT JOIN wp_postmeta stock ON ( wp_posts.ID = stock.post_id AND stock.meta_key = '_stock_status' )
LEFT JOIN wp_term_relationships wtr ON ( wp_posts.ID = wtr.object_id AND stock.meta_value = 'outofstock' )
LEFT JOIN wp_term_taxonomy wtt ON ( wtr.term_taxonomy_id = wtt.term_taxonomy_id AND wtt.taxonomy = 'product_tag' )
LEFT JOIN wp_terms wt ON ( wtt.term_id = wt.term_id AND wt.slug IN( 'antique','reproduction' ) )
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (171) )
AND ( ( wp_postmeta.meta_key = '_visibility' AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog') ) )
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
AND NOT (
stock.meta_value = 'outofstock'
AND ( CASE wt.slug WHEN 'antique' THEN 1 WHEN 'reproduction' THEN 2 ELSE 0 END ) = 0
)
GROUP BY wp_posts.ID, slug_order
ORDER BY wp_posts.post_type DESC, stock.meta_value ASC, slug_order DESC, wp_posts.post_date DESC

MYSQL LEFT OUTER JOIN Query not returning all results

The query below does not return any values for name1 because it does not have wp_postmeta.meta_key = 'wpcf-phone'. I'm using LEFT OUTER JOIN thinking it will return results for name1 as it has values in wp_posts but it's only return values for name2 which does have a wp_postmeta.meta_key = 'wpcf-phone'.
SELECT
wp_posts.ID,
wp_posts.post_content,
wp_posts.post_title,
wp_postmeta.meta_value AS phone
FROM
wp_posts
LEFT OUTER JOIN wp_postmeta wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE
wp_posts.post_type = 'solicitors' AND
wp_posts.post_status = 'publish' AND
wp_postmeta.meta_key = 'wpcf-phone' AND
(wp_posts.post_title LIKE '%name1%' OR
wp_posts.post_title LIKE '%name2%')
You are referencing the outer table in your where clause in this line:
wp_postmeta.meta_key = 'wpcf-phone'
thereby eliminating all rows where wp_postmeta.meta_key is null, effectively turning your LEFT JOIN into an INNER JOIN (since NULL = 'wpcf-phone' is false).
You should move your condition to the join:
SELECT
wp_posts.ID,
wp_posts.post_content,
wp_posts.post_title,
wp_postmeta.meta_value AS phone
FROM
wp_posts
LEFT OUTER JOIN wp_postmeta wp_postmeta
ON (wp_posts.ID = wp_postmeta.post_id)
AND wp_postmeta.meta_key = 'wpcf-phone'
WHERE
wp_posts.post_type = 'solicitors' AND
wp_posts.post_status = 'publish' AND
(wp_posts.post_title LIKE '%name1%' OR
wp_posts.post_title LIKE '%name2%')
When an OUTER JOIN 'fails to connect' it fills the fields in the output with NULL values. You can thus use OR IS NULL to test for this condition. The WHERE-clause should thus be something like:
WHERE
wp_posts.post_type = 'solicitors' AND
wp_posts.post_status = 'publish' AND
(wp_postmeta.meta_key = 'wpcf-phone' OR
wp_postmeta.meta_key IS NULL) AND
(wp_posts.post_title LIKE '%name1%' OR
wp_posts.post_title LIKE '%name2%')

Why Does This SQL INNER JOIN Work and This Doesn't?

I'm still pretty new to SQL, and I'm having trouble wrapping my mind around why one of these queries functions properly and one does not. This is stemming from my attempts to optimize a complex and slow query. #kalengi suggested what looks like a brilliant solution to me, but it doesn't seem to work on my site. Here are the queries.
This is the standard SQL that WordPress generates (this is working as expected):
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
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)
WHERE 1=1
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish')
AND (
(wp_postmeta.meta_key = '_visibility' AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog'))
AND (mt1.meta_key = '_stock_status' AND CAST(mt1.meta_value AS CHAR) = 'instock')
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.menu_order,wp_posts.post_title asc
LIMIT 0, 10
This is the SQL after #kalengi's filter processes it to combine the multiple INNER JOINs into one (this returns 0 results):
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta AS pmta ON (wp_posts.ID = pmta.post_id)
WHERE 1=1
AND wp_posts.post_type = 'product'
AND ( wp_posts.post_status = 'publish' )
AND (
( pmta.meta_key = '_visibility' AND CAST(pmta.meta_value AS CHAR) IN ( 'visible','catalog' ) )
AND ( pmta.meta_key = '_stock_status' AND CAST(pmta.meta_value AS CHAR) = 'instock' )
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.menu_order,wp_posts.post_title asc
LIMIT 0, 10
Can anyone explain whey the second one doesn't work to me?
The two inner joins:
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)
represent two data sets (that happen to be "the same").
Given the WHERE condition, the results will be sets of two rows where the first (wp_postmeta) meets one condition:
(wp_postmeta.meta_key = '_visibility' AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog'))
And the second meets a completely different condition:
(mt1.meta_key = '_stock_status' AND CAST(mt1.meta_value AS CHAR) = 'instock')
By combining everything into one INNER JOIN, you're instead looking for ONE ROW which matches both conditions. Apparently there isn't one.
If you review the "brilliant solution", you'll see that 'AND's were changed to 'OR' to preserve the semantics:
AND (
( pmta.meta_key = '_visibility' AND CAST(pmta.meta_value AS CHAR) IN ( 'visible','catalog' ) )
OR ( pmta.meta_key = '_stock_status' AND CAST(pmta.meta_value AS CHAR) = 'instock' )
)
It looks like you have a condition in your WHERE clause that is looking for two values at the same time in the pmta.meta_key and pmta.meta_value columns:
....
( pmta.meta_key = '_visibility' AND CAST(pmta.meta_value AS CHAR) IN ('visible','catalog' ) )
AND ( pmta.meta_key = '_stock_status' AND CAST(pmta.meta_value AS CHAR) = 'instock' )
....
One column cannot have two different values in the same row, so this test will return FALSE, and consequently, no rows will be returned.
If you rewrite the original query to group the join conditions into the ON clauses, you can see why your second query won't work:
SELECT
SQL_CALC_FOUND_ROWS wp_posts.ID
FROM
wp_posts
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = '_visibility'
AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog')
INNER JOIN wp_postmeta AS mt1
ON wp_posts.ID = mt1.post_id
AND mt1.meta_key = '_stock_status'
AND CAST(mt1.meta_value AS CHAR) = 'instock'
WHERE
1=1
AND wp_posts.post_type = 'product'
AND wp_posts.post_status = 'publish'
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.menu_order,
wp_posts.post_title asc
LIMIT 0, 10
If you want to join the table only once, try something like this:
SELECT
SQL_CALC_FOUND_ROWS wp_posts.ID
FROM
wp_posts
INNER JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
AND (
wp_postmeta.meta_key = '_visibility'
AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog')
) OR (
wp_postmeta.meta_key = '_stock_status'
AND CAST(wp_postmeta.meta_value AS CHAR) = 'instock'
)
WHERE
1=1
AND wp_posts.post_type = 'product'
AND wp_posts.post_status = 'publish'
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.menu_order,
wp_posts.post_title asc
LIMIT 0, 10