Ok, I realize this may be incredibly simple, but my brain is frozen right now. Need a bit of assistance with this query. Let's break it down.
I have two tables (per this example) and I want to update a single table "undeliverable" status
Customers Table (tbl_customers):
+------------+-------------+
| customerID | custAcctNum |
+------------+-------------+
| 1 | 100100121 |
| 2 | 100100122 |
| 3 | 100100123 |
| 4 | 100100124 |
| 5 | 100100125 |
+------------+-------------+
Address Table (tbl_address):
+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
+-----------+------------+---------------+
Dataset with "undeliverable" Customer Account numbers (custAcctNum)
100100121, 100100123, 100100124
And the query will update the Address Table to this
+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 0 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 5 | 0 |
+-----------+------------+---------------+
This is the query that I have tried to use
UPDATE tbl_address
SET undeliverable = 1 WHERE
( SELECT custAcctNum FROM tbl_customers AS c
INNER JOIN tbl_address AS a ON a.customerID = c.customerID )
IN ( 100100121, 100100123, 100100124);
Any suggestions? Thanks!
Use mysql's multiple-table update syntax:
update tbl_Address t
join custAcctNum c
on c.customerid = t.customerid
set t.undeliverable = 1
where c.custAcctNum in (100100121, 100100123, 100100124)
UPDATE tbl_address
SET (undeliverable = 1)
WHERE customerID IN (
SELECT customerID
FROM tbl_customers
WHERE custAcctNum IN (100100121, 100100123, 100100124)
);
Related
table 1: forum_threads
+-----+------+-------+
| id | title| status|
+-----+------+-------+
| 1 | a | 1 |
| 2 | b | 1 |
| 3 | c | 1 |
| 4 | d | 1 |
| 5 | e | 1 |
| 6 | f | 1 |
+-----+------+-------+
table 2: forum_comments
+-----+----------+--------------------+
| id | thread_id| comment |
+-----+----------+--------------------+
| 1 | 4 | hai |
| 2 | 4 | hello |
| 3 | 2 | welcome |
| 4 | 2 | whats your name |
| 5 | 6 | how are you |
| 6 | 5 | how old are you |
| 7 | 5 | good |
+-----+----------+--------------------+
wanted output
+-----------+----------+-----------------+
| thread_id | title | comment_count |
+-----------+----------+-----------------+
| 5 | e | 2 |
| 6 | f | 1 |
| 2 | b | 2 |
| 4 | d | 2 |
+-----------+----------+-----------------+
my Query
SELECT forum_threads.*,forum_comments.*,count(forum_comments.id) as comment_count
FROM forum_comments
LEFT JOIN forum_threads ON forum_comments.thread_id = forum_threads.id
GROUP BY forum_threads.id
ORDER BY forum_comments.id desc
Here I am trying to get the titles by the latest comment.
when I give ORDER BY forum_comments.id this returns the wrong order.
I need to order by the latest comments in the forum_comments table.
this query returns the wrong order please help me to find out the correct order.
how could I solve this easily?
This query should give you the expected result:
select t2.thread_id, t1.title, t2.comment_count from forum_threads as t1,
(SELECT id, thread_id, count(comment) as comment_count from forum_comments group by thread_id) as t2
where t1.id = t2.thread_id order by t2.id desc;
Instead of using forum_threads.* and forum_comments.* can you give specific column names and try.
If that doesn't work you should try explicitly assigning primary and foreign keys.
I have a table in my application which maps products to categories product_to_category:
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | 5 |
| 2 | 4 |
| 3 | 7 |
| 4 | 8 |
| 4 | 1 |
| 5 | 10 |
+------------+-------------+
Now I have to create another table similar to this but with updated category_ids based on a third mapping table:
new_product_to_category
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 4 | |
| 5 | |
+------------+-------------+
new_mapping
+----------+---------+
| old_cats | new_cat |
+----------+---------+
| 1,2,3 | 91 |
| 12,11 | 92 |
| 55,23 | 93 |
| 43,78 | 94 |
| 4,5,6 | 95 |
| 33,37,39 | 96 |
+----------+---------+
If I could do a join with IN clause then I would do something like this:
INSERT INTO new_product_to_category (product_id, category_id)
SELECT pc.product_id, nm.new_cat
FROM product_to_category as pc
JOIN new_mapping as nm
ON pc.category_id IN nm.old_cats
But I guess I can't use IN clause in JOIN, how I can achieve the same functionality?
You could do an update join with the help of FIND_IN_SET:
UPDATE new_product_to_category t1
INNER JOIN new_mapping t2
ON FIND_IN_SET(t1.product_id, t2.old_cats) > 0
SET t1.category_id = t2.new_cat;
If you really wanted to avoid using a join, you could also phrase this update using a correlated subquery:
UPDATE new_product_to_category t1
SET category_id =
(SELECT t2.new_cat FROM new_mapping t2
WHERE FIND_IN_SET(t1.product_id, t2.old_cats) > 0);
I have two tables tbl_user1 and tbl_user2 both are field name are same but there is no relation between that tables now I want to find total referred count from both table for example...
tbl_user1
-----------------------
UID | referenceBy | firstName | lastName | emailAddress
----------------------------------------------------------------------------
1 | NULL | aa1 | ab1 | aa1#email.com
2 | aa1#email.com | aa2 | ab2 | aa2#email.com
3 | NULL | aa3 | ab3 | aa3#email.com
4 | aa2#email.com | aa4 | ab4 | aa4#email.com
5 | aa2#email.com | aa5 | ab5 | aa5#email.com
6 | bb1#email.com | aa6 | ab6 | aa6#email.com
7 | bb2#email.com | aa7 | ab7 | aa7#email.com
8 | bb3#email.com | aa8 | ab8 | aa8#email.com
9 | bb3#email.com | aa9 | ab9 | aa9#email.com
and second one table is somthing like...
tbl_user2
-----------------------
UID | referenceBy | firstName | lastName | emailAddress
----------------------------------------------------------------------------
1 | NULL | bb1 | bc1 | bb1#email.com
2 | bb1#email.com | bb2 | bc2 | bb2#email.com
3 | NULL | bb3 | bc3 | bb3#email.com
4 | bb3#email.com | bb4 | bc4 | bb4#email.com
5 | bb2#email.com | bb5 | bc5 | bb5#email.com
6 | bb1#email.com | bb6 | bc6 | bb6#email.com
7 | aa2#email.com | bb7 | bc7 | bb7#email.com
8 | aa3#email.com | bb8 | bc8 | bb8#email.com
9 | bb5#email.com | bb9 | bc9 | bb9#email.com
now, as you can see there is no relation between these two tables and I want result like following..
MAIN_RESULT_THAT_I_WANT
-----------------------
referenceEmail | referenceEmailCount
----------------------------------------------------------------------------
aa1#email.com | 1
aa2#email.com | 3
aa3#email.com | 1
aa4#email.com | 0
aa5#email.com | 0
aa6#email.com | 0
aa7#email.com | 0
aa8#email.com | 0
aa9#email.com | 0
bb1#email.com | 3
bb2#email.com | 2
bb3#email.com | 3
bb4#email.com | 0
bb5#email.com | 1
bb6#email.com | 0
bb7#email.com | 0
bb8#email.com | 0
bb9#email.com | 0
here in result all emailAddress of all user and total of how many user(s) registered by that particular emailAddress.
I am guessing that the result you want is just copy and pasted since it seems inaccurate. Like HoneyBadger says it is strange that aa6 is missing and still in the result, that indicates you have another list you are not telling us about? Or you just write the result in notepad...
If you just want a list of emails and count this will work:
select referenceBy, count(1) as referenceEmailCount from (
select referenceBy from tbl_user1
union all
select referenceBy from tbl_user2
) as t
group by referenceBy
Otherwise give us more info if this is not what you need.
Since the schema is same for 2 tables so you can perform union to get combined results and can perform an outer query to get the total count.
select referenceEmail, count(*) as referenceEmailCount from (
select * from table1
union all
select * from table2
) as alias
group by alias.referenceEmail
I'm getting crazy to create an insert to a table from a select of same model.
I think that I should create a temp table to elaborate all model, but how?
Here the table I have:
Table product
+------------+----------+
| id_product | id_model |
+------------+----------+
| 1 | D |
| 2 | D |
| 3 | A |
| 4 | C |
| 5 | D |
+------------+----------+
Here the table I would like to create, but my best problem is that the record in related should be duplicate like
id1->id2 and id2->id1
Table related
+------------+-----------------+
| id_product | related_product |
+------------+-----------------+
| 1 | 2 |
| 1 | 5 |
| 2 | 1 |
| 2 | 5 |
| 5 | 1 |
| 5 | 2 |
+------------+-----------------+
Hmmm. I think you just want a self-join
select p1.id_product, p2.id_product as related_product
from product p1 join
product p2
on p1.id_model = p2.id_model and
p1.id_product <> p2.id_product;
Three table as follow
mysql> select * from food;
+--------+------+-------+
| foodid | name | price |
+--------+------+-------+
| 1 | 雞 | 100 |
| 2 | 鴨 | 200 |
| 3 | 魚 | 300 |
| 4 | 肉 | 400 |
+--------+------+-------+
4 rows in set
mysql> select * from drink;
+---------+------+-------+
| drinkid | name | price |
+---------+------+-------+
| 1 | 紅茶 | 50 |
| 2 | 綠茶 | 100 |
| 3 | 奶茶 | 150 |
+---------+------+-------+
3 rows in set
mysql> select * from order_table;
+----+-----------+--------+---------+------------+-------------+-------------+
| id | user_name | foodid | drinkid | food_count | drink_count | total_price |
+----+-----------+--------+---------+------------+-------------+-------------+
| 2 | 小明 | 3 | 2 | 2 | 2 | 0 |
| 3 | 小華 | 1 | 1 | 1 | 8 | 0 |
| 4 | 小英 | 1 | 3 | 3 | 3 | 0 |
| 6 | 小a | 2 | 1 | 4 | 6 | 0 |
| 7 | 小b | 2 | 2 | 5 | 4 | 0 |
| 8 | 小c | 2 | 3 | 6 | 10 | 0 |
| 9 | 大A | 3 | 1 | 9 | 8 | 0 |
| 10 | 大B | 3 | 2 | 5 | 4 | 0 |
| 11 | 大C | 3 | 3 | 10 | 3 | 0 |
+----+-----------+--------+---------+------------+-------------+-------------+
foodid in order_table is link to foodid in food table,
drinkid in order_table is link to drinkid in drink table,
Now, I want to calculate total price,
Total_price =
order_table.foodid(food.price in food table) * order_table.food_count +
order_table.drinkid(drink.price in drink table) * order_table.drink_count;
So, let me knowlege the command to update total price
thx a lot.
The reason why I used LEFT JOIN on the following query is because I assumed that some orders may only contain drinks or foods.
UPDATE order_table a
LEFT JOIN food b
ON a.foodid = b.foodID
LEFT JOIN drink c
ON a.drinkID = c.drinkID
SET a.total_price = (a.food_count * COALESCE(b.price, 0) +
a.drink_count * COALESCE(c.price, 0))
SQLFiddle Demo
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Something like this should be close:
SELECT COALESCE(F.Price,0)*OT.Food_Count+COALESCE(D.Price,0)*OT.Drink_Count Total_price
FROM Order_Table OT
LEFT JOIN Food F ON OT.FoodId = F.FoodId
LEFT JOIN Drink D ON OT.DrinkId = D.DrinkId
And to actually update that column:
UPDATE Order_Table OT
LEFT JOIN Food F ON OT.FoodId = F.FoodId
LEFT JOIN Drink D ON OT.DrinkId = D.DrinkId
SET OT.Total_Price = COALESCE(F.Price,0)*OT.Food_Count+COALESCE(D.Price,0)*OT.Drink_Count