Having trouble finding workable syntax to perform this query
SELECT *
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.term_id IN (307)
GROUP BY wp_posts.ID
Works fine.
What I'm trying to do is add this
WHERE wp_posts.title NOT LIKE '%lug'
I've tried many variations.. but they all result in error.
Tried
AND WHERE wp_posts.title NOT LIKE '%lug'
after the second WHERE
I've also tried adding it before the LEFT JOIN
Just getting a generic syntax error wherever I try to place it in the query.
the condition of left joined table should be added to on clause and not in where otherwise works as inner join
SELECT *
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
and wp_term_taxonomy.term_id IN (307)
WHERE wp_posts.title NOT LIKE '%lug'
GROUP BY wp_posts.ID
you should not use group by if you don'have aggregation function (use disticnt if yoy neeed) this behavior is deprecated in sql and in some version is not more allowed
Related
I am trying to change posts using MySql Quariy and keep getting the 1093 Err
I have tried all the tips about using a sub queries with no success
maybe somebody can help me out
UPDATE wp_posts SET post_content = CONCAT(post_content , 'testext_bla_bla')
WHERE wp_posts.id in (select wp_posts.ID
from wp_posts
LEFT JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_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 wp_posts.post_excerpt like '%LO1185LH%' and wp_term_taxonomy.term_taxonomy_id = 27)
and keep getting the "#1093 - You can't specify target table 'wp_posts' for update in FROM clause "
thanks
asaf
Use a alias for you updated table;)
UPDATE wp_posts t1 SET t1.post_content = CONCAT(t1.post_content, 'testext_bla_bla')
WHERE t1.id IN (
SELECT wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_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 wp_posts.post_excerpt LIKE '%LO1185LH%' AND wp_term_taxonomy.term_taxonomy_id = 27)
Or you can just use LEFT JOIN to do this;)
UPDATE wp_posts t1
LEFT JOIN wp_term_relationships ON t1.ID = wp_term_relationships.object_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
SET t1.post_content = CONCAT(t1.post_content, 'testext_bla_bla')
WHERE t1.post_excerpt LIKE '%LO1185LH%' AND wp_term_taxonomy.term_taxonomy_id = 27
It's because the update sentences don't have FROM, because you set the table to update after UPDATE clause, like:
UPDATE wp_posts
SET column1=value, column2=value2,...
WHERE some_column = some_value.
database engine is taking the FROM clause as it's inside UPDATE sentence (because it's an update sentence) the easiest way to handle it is to make a SELECT first (with php) and use this result to handle it on update, like:
$result = connection()->query("SELECT blah_blah_blah");
connection()->close();
$rs = mysqli_fetch_array($result);
//ok you got the result on $rs, if you wanna do an update with this it should
//be unique result, if you want to do it recursively, simply act over $rs[$i];
//then, once or in a loop:
$sql = "UPDATE wp_posts
SET column1=value, column2=value2,...
WHERE some_column = ".$rs['index_that_you_want'].";";
connection()->query($sql);
(remember to take the next row using $rs = mysqli_fetch_array($result); at the bottom of a loop if you need to run it recursively.
I have to make a mysql select from a wordpress database :
SELECT wp_posts.ID, wp_posts.post_title AS post_title,
responsable.meta_value AS responsable,
nif_cliente.meta_value AS nif_cliente,
CONCAT_WS(' ',contactos_0_nombre.meta_value,CONCAT('<br><br>',contactos_1_nombre.meta_value)) AS contactos,
tipo_de_empresa.meta_value AS tipo_de_empresa,
tipo_cliente.meta_value AS tipo_cliente
FROM wp_posts
LEFT JOIN wp_postmeta AS responsable
ON wp_posts.ID = responsable.post_id AND responsable.meta_key='responsable'
LEFT JOIN wp_postmeta AS tipo_de_empresa
ON wp_posts.ID = tipo_de_empresa.post_id AND tipo_de_empresa.meta_key='tipo_de_empresa'
LEFT JOIN wp_postmeta AS nif_cliente
ON wp_posts.ID = nif_cliente.post_id AND nif_cliente.meta_key='nif_cliente'
LEFT JOIN wp_postmeta AS contactos_0_nombre
ON wp_posts.ID = contactos_0_nombre.post_id AND contactos_0_nombre.meta_key='contactos_0_nombre'
LEFT JOIN wp_postmeta AS contactos_1_nombre
ON wp_posts.ID = contactos_1_nombre.post_id AND contactos_1_nombre.meta_key='contactos_1_nombre'
LEFT JOIN wp_postmeta AS tipo_cliente
ON wp_posts.ID = tipo_cliente.post_id AND tipo_cliente.meta_key='tipo_cliente'
WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'clientes'"
I want to concate contactos_n_nombre. I can write it manual but 'n' it's dinamic (i don't know how much it is) It can be 0 if there are only one contact, but can be 1000 if this client have more contact methods.
How can i make it to search how many contactos have one client, and concate all of them
Thanks
If I understand your question correctly, you can use the MySQL GROUP_CONCAT function. You may need to adjust this for your situation, but the basic idea is:
Set up the database JOIN as:
LEFT JOIN wp_postmeta AS contactosdb ON wp_posts.ID = contactosdb.post_id
Then use the following in your SELECT:
GROUP_CONCAT(contactosdb.meta_value SEPARATOR '<br><br>') AS contactos
I'm using the following SQL query to get the records I need from a Wordpress database.
SELECT p.id, p.post_title, m.meta_key, m.meta_value, t.slug
FROM wp_posts p
INNER JOIN wp_postmeta m ON p.id=m.post_id
AND m.meta_key='_subscription_sign_up_fee'
INNER JOIN wp_term_relationships AS tr ON p.id = tr.object_id
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id
WHERE t.slug = 'bundle'
This pulls the correct records. My issue is that I need to increase the value of the _subscription_sign_up_fee by 100.
I've tried the following
update wp_postmeta
set wp_postmeta.meta_value = wp_postmeta.meta_value + 100
INNER JOIN wp_postmeta m ON wp_posts.id=m.post_id
AND m.meta_key='_subscription_sign_up_fee'
INNER JOIN wp_term_relationships AS tr ON wp_posts.id = tr.object_id
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id
WHERE t.slug = 'bundle'
and get an error saying that 'Column 'meta_value' in field list is ambiguous '
I'm pretty sure I'm not understanding the aliases. Can someone point me in the right direction?
The correct syntax for MySQL UPDATE JOINs is to have SET at the end. You also have some problems in that you're joining wp_postmeta twice, but lost the join with wp_posts.
Something like this should work;
UPDATE wp_postmeta m
JOIN wp_posts
ON wp_posts.id = m.post_id AND m.meta_key = '_subscription_sign_up_fee'
JOIN wp_term_relationships AS tr
ON wp_posts.id = tr.object_id
JOIN wp_term_taxonomy AS tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms AS t
ON tt.term_id = t.term_id
SET m.meta_value = m.meta_value + 100
WHERE t.slug = 'bundle'
...and always remember to back up your data before running potentially destructive operations from random people on the Internet :)
I need to select some things from few WP tables, one of which is custom field value with a certain key. Problem is, some of posts don't have custom field with this key but aside of that they match query. So question is, how do I select even posts that doesn't have custom field with this key, but other than that match query? Value of field for the posts that do not have custom field should be set 0. I figured it's something like COALESCE, but I'm either using it wrong or it can't be used in this case. My query so far is:
select lists.id,lists.`type`,lists.`status`,lists.watched_eps, lists.score,wp_posts.post_title,wp_posts.post_name,wp_terms.slug,wp_postmeta.meta_value from anime_lists
INNER JOIN wp_posts ON (wp_posts.ID = lists.post_id )
INNER JOIN wp_postmeta ON (wp_postmeta.post_id= lists.post_id )
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
INNER JOIN wp_terms ON(wp_terms.term_id = wp_term_taxonomy.term_id)
where lists.id=1
and wp_term_taxonomy.taxonomy = 'category'
and wp_postmeta.meta_key='eps'
group by lists.`post_id`
order by lists.`status`, wp_posts.post_title
As it is, query outputs everything that I need but without these posts that do not have custom field 'eps'. If I remove and wp_postmeta.meta_key='eps' it outputs these posts, but that way I get wrong value in wp_postmeta.meta_value field.
As far as I understand what you want, you can just change your wp_postmeta join to a LEFT JOIN and move the WHERE condition on that table to the join, leaving;
SELECT lists.id, lists.`type`, lists.`status`, lists.watched_eps,
lists.score,wp_posts.post_title,wp_posts.post_name,wp_terms.slug,
wp_postmeta.meta_value
FROM anime_lists
INNER JOIN wp_posts
ON wp_posts.ID = anime_lists.post_id
LEFT JOIN wp_postmeta
ON wp_postmeta.post_id = anime_lists.post_id
AND wp_postmeta.meta_key='eps'
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy
ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
INNER JOIN wp_terms
ON(wp_terms.term_id = wp_term_taxonomy.term_id)
WHERE lists.id=1
AND wp_term_taxonomy.taxonomy = 'category'
GROUP BY lists.`post_id`
ORDER BY lists.`status`, wp_posts.post_title
I have this working query here that will retrieve tags used in the last 60 days.
$term_ids = $wpdb->get_results("
SELECT DISTINCT wp_term_taxonomy.term_id, wp_terms.name
FROM wp_term_taxonomy
INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'post_tag' AND DATE_SUB(CURDATE(), $show_tags_in_days) <= $wpdb->posts.post_date", ARRAY_A);
What I need to modify this to is tags used in the last 60 days AND in a specific Wordpress category. This will be used on a category page of Wordpress. I've been fiddling with this for awhile and I think I've just got myself all confused. Is there a way to do this with another join or should I run two separate queries?
I managed to figure it out. Here is the query I came up with.
SELECT DISTINCT wp_term_taxonomy.term_id, wp_terms.name FROM wp_terms
INNER JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id=wp_terms.term_ID
INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID=wp_term_relationships.object_id
WHERE wp_term_taxonomy.taxonomy = 'post_tag' AND wp_posts.ID IN (
SELECT wp_posts.ID FROM wp_terms
INNER JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id=wp_terms.term_ID
INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID=wp_term_relationships.object_id
WHERE wp_term_taxonomy.taxonomy = 'category' AND wp_terms.term_id=$cur_cat_id) LIMIT 10
Here is a more in-depth explanation: http://www.ohthecode.com/wordpress/most-used-tags-in-wordpress/
This query retrieves only taxonomy IDs. It is likely that you are using the $term_ids variable in another query to retrieve the posts that have both a tag from the list in the $term_ids variable and are in the category. In other words, find the place where you are actually retrieving posts and pass in both the $term_ids and the category id you are interested in.
However, since you say that you are doing this on a category template page (say category-mycategory.php), you may not need to do anything additional at all since the category should be passed in automatically.