Ok it has been a while since I last wrote an sql query. I have the following query
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = 'select_team_leader'
AND {$wpdb->usermeta}.meta_value = {$user_leader})",
$user_search->query_where
);
I want to add an OR statement like
OR {$wpdp->usermeta}.user_id = {$user_leader}
Do I need to use an inner join for this ? thanks
No you don't need to use an extra join you just have to organize your query to look for either user id or meta_value
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = 'select_team_leader'
AND
(
{$wpdb->usermeta}.meta_value = {$user_leader}
OR {$wpdp->usermeta}.user_id = {$user_leader}
)
)",
$user_search->query_where
);
So above query will look for meta_value is equal to provided value ie. {$user_leader} or user_id is equal to {$user_leader} but whatever matches meta_key should be select_team_leader
Edit from comments
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE (
{$wpdb->usermeta}.meta_key = 'select_team_leader'
AND {$wpdb->usermeta}.meta_value = {$user_leader}
)
OR {$wpdp->usermeta}.user_id = {$user_leader}
)",
$user_search->query_where
);
Related
I am trying to UPDATE table value where meta key value _stock_status and id value list of ids, but i got an error (You can't specify target table 'wpp' for update in FROM clause). please advise here is my query
UPDATE meta_post AS wpp
SET wpp.meta_value = 'instock'
WHERE wpp.meta_key = '_stock_status'
AND wpp.id IN (
SELECT DISTINCT id
FROM meta_post
WHERE meta_key = '_stock'
AND (meta_value BETWEEN 2 AND 4)
)
thank you
Need to update column value where meta_key = '_stock_status' and id = [1,2,3,4]
I found solution, below query works fine for me.
UPDATE meta_post AS wpp
SET wpp.meta_value = 'instock'
WHERE wpp.meta_key = '_stock_status'
AND wpp.id IN (
SELECT * FROM (
SELECT DISTINCT id FROM meta_post
WHERE meta_key = '_stock'
AND (meta_value BETWEEN 2 AND 4)
) AS pids
)
Thanks for your valuable time.
UPDATE meta_post AS wpp
JOIN (
SELECT id
FROM meta_post
WHERE meta_key = '_stock'
AND meta_value BETWEEN 2 AND 4
) tmp USING (id)
SET wpp.meta_value = 'instock'
WHERE wpp.meta_key = '_stock_status'
I am working with sql query to get post ids of the product based on multiple table joining which working ok, Now I am try add the sort based on price, price is save in the wp_postmeta
table as meta_key and value. Problem is joining have already condition where we are using different set of meta_key and value. And I am not clear how to add the sort on it, what ever attempt I did I get zero result.
Here is the original query which is working ok.
$args_sql = "SELECT wordpress_posts.ID as ID FROM wordpress_posts INNER JOIN wordpress_term_relationships ON (wordpress_posts.ID = wordpress_term_relationships.object_id) INNER JOIN wordpress_postmeta ON (wordpress_posts.ID = wordpress_postmeta.post_id) INNER JOIN wordpress_postmeta AS mt1 ON (wordpress_posts.ID = mt1.post_id) INNER JOIN wordpress_postmeta AS mt2 ON (wordpress_posts.ID = mt2.post_id) WHERE 1 = 1 AND (wordpress_term_relationships.term_taxonomy_id IN ($txt1)) AND wordpress_posts.post_author NOT IN ($disable_store_txt) AND (wordpress_postmeta.meta_key = 'my_pricing_structure' AND (mt1.meta_key = 'meta_product_type' AND mt1.meta_value != 'auction') AND ((mt2.meta_key = 'meta_ship_to_country_1' AND mt2.meta_value = 'india') OR (mt2.meta_key = 'meta_ship_to_country_2' AND mt2.meta_value = 'india') OR (mt2.meta_key = 'meta_ship_to_country_3' AND mt2.meta_value = 'india') OR (mt2.meta_key = 'meta__ship_to_country_4' AND mt2.meta_value = 'india'))) AND wordpress_posts.post_type = 'my-products' AND ((wordpress_posts.post_status = 'publish')) GROUP BY mt2.post_id ORDER BY wordpress_posts.post_date DESC";
now I add sort on meta_key "original_price"
I'm trying to make an Sql , that get all data from first table "posts_main" and then get the count of comments from second table "posts_comments"
I tried:
$sql = "SELECT * FROM posts_main, count(posts_comments.groupid)
INNER JOIN posts_comments ON posts_comments.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'
GROUP BY posts_main.id";
Please, how can i do that ?
thanks....
Try with below query.
$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_group
from posts_main
WHERE posts_main.user_id = '$user_id' ";
Try this with subquery
SELECT posts.*,cnt FROM posts_main
INNER JOIN (select posts_comments.groupid,count(posts_comments.groupid) as cnt
group by posts_comments.groupid)a
ON a.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'
I have two tables ... accounts & tax. In table tax I would like to updated records (?) depending on column category available in table accounts like this:
table 'accounts'
id|cat2|cat3
1|A|Active
2|A|Active
3|A|Inactive
4|A|Active
5|B|Inactive
6|B|Active
table 'tax'
id|category|count_total|count_active|count_inactive
1|A|?|?|?
2|B|?|?|?
Desired result:
id|category|count_total|count_active|count_inactive
1|A|4|3|1
2|B|2|1|1
For count_total I tried this:
UPDATE tax t2,
( SELECT count(*)
FROM accounts
) t1
SET t2.count_total = t1.count(*)
WHERE t1.cat2 = t2.category;
You can use join to update your tax table
UPDATE tax t2
JOIN (
SELECT
cat2,
COUNT(*) cnt,
SUM(cat3 = 'Active') count_active,
SUM(cat3 = 'Inactive') count_inactive
FROM
accounts
GROUP BY cat2
) t1
ON t1.cat2 = t2.category
SET t2.count_total = t1.cnt ,
t2.count_active = t1.count_active,
t2.count_inactive = t1.count_inactive
Demo
Another version without JOIN:
UPDATE tax
SET
count_total = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category
),
count_active = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category AND accounts.cat3 = 'Active'
),
count_inactive = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category AND accounts.cat3 = 'Inactive'
);
Demo
I have a left join select statement that I want to select specific columns based on the value of a variable that has either a '0' or a '1'. I thought I could implement this using CASE, but I don't seem to be having any luck.
If $variable contains a '0' I want my select statement to retrieve only users.user, table2.total1 and table2.total2, but if the $variable contains a '1' I want to select only users.user and table2.total.
$value = '1';
$conn->query(
"select users.user, table2.total, table2.total1, table2.total2
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");
Does anyone have any ideas? Thanks.
You don't need to do this with sql, if in fact the query differ only by the select clause you could use a conditional and a variable:
$value = '1';
if($variable == '1') {
$select = "select users.user, table2.total1, table2.total2 ";
}
else {
$select = "select users.user, table2.total ";
}
$conn->query(
$select . "
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");