SQL: Selecting value of cell based on the value of another cell - php

The WordPress database schema for Users is as follows:
TABLE wp_users
ID
user_login
user_pass
user_nicename
user_email
...
TABLE wp_usermeta
umeta_id
user_id
meta_key
meta_value
Now, it's the wp_usermeta table that I want to be able to write a SQL statement for.
In one row, meta_key is "first_name" ... how do I write a SQL query that will pull out the meta_value where meta_key is equal to "first_name"? This does not work:
SELECT meta_value FROM wp_usermeta WHERE meta_value = "first_name";

It should be
SELECT meta_value FROM wp_usermeta WHERE meta_key = "first_name";
Notice you had WHERE meta_value instead of WHERE meta_key

Related

Insert meta_key if another meta_key exist

I trying to copy data from one product feed plugin to another. First thing I have to do is create a empty meta_key '_woosea_brand' but only to this posts where another meta_key 'woocommerce_ceneo_Producent' exist. Later i will copy the meta_value (already figure it out ). The problem is to insert the empty meta_key = '_woosea_brand" if meta_key = 'woocommerce_ceneo_Producent' exist.
I was trying this code: I found it in another stackoverflow post. But it change meta_value.
PhPmyadmin SQL Query to insert meta_key & value IF another value exist
INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT "_wplp_post_front", 1, post_id
FROM wp_postmeta outside_table
WHERE meta_key = 'qty'
AND meta_value = '0'
AND NOT EXISTS
(SELECT * FROM wp_postmeta
WHERE post_id = outside_table.post_id
AND meta_key = "_wplp_post_front"
AND meta_value = 1
)
OK i think I found solution:
INSERT INTO wplw_postmeta (post_id, meta_key, meta_value)
SELECT post_id, '_woosea_brand', meta_value FROM wplw_postmeta WHERE meta_key='woocommerce_ceneo_Producent'

How to update rows when WHERE value is a sub query with more than 1 row of results

I want to run this SQL:
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE meta_key = '_test'
AND post_id = (SELECT post_id
FROM wp_postmeta
WHERE meta_value = 8032)
I was hoping it would set meta_value to 1.00 where the meta_key is _test and post_id is any of the returned post_id from the sub query: SELECT post_id FROM wp_postmeta WHERE meta_value = 8032
Upon simulating this query in phpMyAdmin it says that the sub query has more than 1 result, therefore how can I make this work?
Maybe it's not really something to do in SQL, I guess I could pull the post_id sub query results into PHP then loop through them and then do the original SQL..?
Replace = with IN:
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE
meta_key = '_test'
AND post_id IN (SELECT post_id FROM wp_postmeta WHERE meta_value = 8032)
But as table is the same why not just:
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE
meta_key = '_test'
AND meta_value = 8032
Error
Table 'wp_postmeta' is specified twice, both as a target for 'UPDATE' and as a separate source for data
can be fixed as
UPDATE wp_postmeta
SET meta_value = 1.00
WHERE
meta_key = '_test'
AND post_id IN (
SELECT * FROM (
SELECT post_id FROM wp_postmeta WHERE meta_value = 8032
) t
)

wpdb - add up values for custom field within a specific category

I have a custom field called "points." This is a numerical value, so some posts have 2 points, some have 10, etc.
I want to count the total number of "points" for all of the posts within a given category who also have the custom field "cf_Category" with the value of "Chapter Building"
So, maybe query all the posts in Category X, whose value for meta key "cf_Category" is "Chapter Building" and then add up the values for the meta key "Points" and then echo that number.
Does that make sense? I'm able to add up all of the points for all posts, but am having trouble limiting it to one category.
Thanks in advance!
Sounds like you need a subquery. In SQL, you want something like this:
SELECT sum(meta_value) from wp_postmeta
WHERE meta_key = "points"
AND post_id in (SELECT object_id from wp_term_relationships
WHERE term_taxonomy_id = 6)
AND post_id in (SELECT post_id from wp_postmeta
WHERE meta_key = "cf_Category" and meta_value = "Chapter Building")
The inner query will return a list of post_ids that in the taxonomy with id 6. Those post ids are then used as a condition for the query that sums the points field.
For reference, the object_id field in the wp_term_relationships table is generally the same as a post's ID, and the term_taxonomy_id in that table is generally the same as the term_id. If you don't know the term_id of a category or tag, you can find it by looking at the URL of the page when you edit that category; it's in there as a parameter called "tag_ID".
If you're not familiar with writing custom SQL queries in Wordpress, the Codex has some details. You'll want to use $wpdb->prepare().
Final code per OP
<?php
$meta_key = 'cf_PointValue';
$point_total = $wpdb->get_var( $wpdb->prepare(
"
SELECT sum(meta_value) from wp_postmeta
WHERE meta_key = %s
AND post_id in (SELECT object_id from wp_term_relationships
WHERE term_taxonomy_id = 17)
AND post_id in (SELECT post_id from wp_postmeta
WHERE meta_key = 'cf_Category' and meta_value = 'Chapter Building')",
$meta_key
) );
echo $point_total;
?>
<?php
$meta_key = 'cf_PointValue';
$point_total = $wpdb->get_var( $wpdb->prepare(
"
SELECT sum(meta_value) from wp_postmeta
WHERE meta_key = %s
AND post_id in (SELECT object_id from wp_term_relationships
WHERE term_taxonomy_id = 17)
AND post_id in (SELECT post_id from wp_postmeta
WHERE meta_key = 'cf_Category' and meta_value = 'Chapter Building')",
$meta_key
) );
echo $point_total;
?>

Need advice on retrieving data from DB

I have a MySQL DB with 2 tables.
First table:
Posts
ID
post_title
post_content
... etc
And second table with additional data:
Postmeta
ID
Post_ID (FK)
meta_key
meta_value
The data that I need is in the meta_key/value pairs. But this DB organized in the way that each row in the second table only have one piece of information.
For example (second table):
ID Post_ID meta_key meta_value
1 5 info 30
ID Post_ID meta_key meta_value
2 5 additional_info 40
ID Post_ID meta_key meta_value
3 6 info 50
ID Post_ID meta_key meta_value
4 6 additional_info 60
So if I run query
SELECT
posts.id,
posts.post_date,
posts.post_author,
posts.post_content,
posts.post_title,
postmeta.meta_id,
postmeta.meta_key,
postmeta.meta_value
FROM posts INNER JOIN postmeta ON posts.id = postmeta.post_id
WHERE posts.post_type = 'post'
I get back all data that I need but data from first table (ID/post_content etc) repeated many times.
But I only need data from first table once but all additional data from second table related to ID from the first table.
How I should filter all this stuff? May be some php loop?
You can use a mysql group_concat function like this:
select
posts.id,
posts.post_date,
posts.post_author,
posts.post_content,
posts.post_title,
group_concat(postmeta.meta_id) as metaID,
group_concat(postmeta.meta_key) as metaKey,
group_concat(postmeta.meta_value_ as metaValue
from
posts
INNER JOIN postmeta
ON posts.id = postmeta.post_id
where
posts.post_type = 'post'
group by
posts.id,
posts.post_date,
posts.post_author,
posts.post_content,
posts.post_title
The to split out the results in PHP you can easily use an explode function to form an array of the resulting information.
SELECT * FROM Posts, Postmeta
WHERE Posts.Post_id = Postmeta.Post_id
GROUP BY Posts.Post_id
Use SELECT DISTINCT instead of just SELECT

Sql query to show some results using PHP

at the end of this I want to display the TITLES of the results that have extra1 as meta_key and test as meta_value.
My goal is to:
From table wp_postmeta get the post_id of every row that has extra1 as meta_key and test as meta_value
Using the post_id, get the post_title for every row on step 1 respectively.
Print the post_title
Thank you very much.
this is the query assuming you are working on wordpress database schema:
SELECT post_title
FROM wp_posts
WHERE ID IN
(
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'extra1' AND meta_value = 'test'
)
second query after Mairy's comment:
SELECT post_title
FROM wp_posts
WHERE ID IN
(
SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key IN('extra1','extra2','extra3') AND meta_value IN('test','test1','test2','test3')
)
then you just need to loop over th result set with php and then print the post title as you prefer:
//connect to DB then ($query contains the query above)
$res = mysql_query($query, $db);
while($row=mysql_fetch_array($res))
{
echo($row['post_title']);
}

Categories