I'm trying to perform a UPDATE JOIN query in MySQL
I need to do the following: Add the table_1.won to table_2.total_winnings for a given session
+++ Table_1 +++
--id-- --name-- --selection-- -potential_winnings-- -- won -- --session--
1 John a 67 0 1
2 Jame b 10 **10** 1
3 David c 43 0 1
4 Sam b 20 **20** 1
5 Alex b 30 **30** 1
6 Rob b 1000 0 2
+++ Table_2 +++ (BEFORE)
--id-- --Total_winnings-- -- session --
1 4534 1
2 885 1
3 0 1
4 5 1
5 10 1
6 5465 2
My desired output is below
input : winning selection = b
session =1
+++ Table_2 +++ (AFTER)
--id-- --Total_winnings-- -- session --
1 4534 1
2 **895** 1
3 0 1
4 **25** 1
5 **40** 1
6 5465 2
I can do this by selecting each user from table_1 who has won and looping over there entry in table_2, but I have a large number of items to process now, so I think I need a join of somesort to accomplish this..
I'm currently doing
UPDATE table_2 SET Total_winnings = Total_winnings + 10 WHERE id = 2 AND session = 1
If anyone would know how to do this, or has a simple example of a SQL join with and UPDATE query that would be most useful. I have seen other examples of this, but I can never figure out what it going on in the SQL!!
You're looking for something like this?
UPDATE table_2
join table_1 on table_1.id = table_2.id
SET Total_winnings = Total_winnings + won
WHERE session = 1 and selection = 'b'
Related
I have two tables and need to swap the values of a column in each table - I can do this when they are in the same table but when I try to do this with different tables then the second value is already overwritten so gets lost.
For example:
table1
id user_id currency col2 col3......
1 1 10 Bob 2018-04-16
2 2 150 Tom 2018-05-17
3 3 60 Phil 2018-06-04
4 4 125 Jon 2017-12-01
5 5 35 Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 USD 16 Active
2 2 USD 17 Active
3 3 GBP 21 Left
4 4 CAD 16 Active
5 5 AUD 19 Active
I need these to look like:
table1
id user_id currency col2 col3......
1 1 USD Bob 2018-04-16
2 2 USD Tom 2018-05-17
3 3 GBP Phil 2018-06-04
4 4 CAD Jon 2017-12-01
5 5 AUD Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 10 16 Active
2 2 150 17 Active
3 3 60 21 Left
4 4 125 16 Active
5 5 35 19 Active
I tried:
UPDATE table1 t1, table2 t2
SET t1.currency=t2.salary, t2.salary=t1.currency
WHERE t1.user_id=t2.user_id;
but this does not work (currency gets set correctly but not the salary), is it possible to do?
Swap two columns values between two tables looked like a possible solution but the solution is changing table names as all the columns need swopped whereas I only need single columns swapped.
I believe you'll need to use a mix of both DDL and DML to do this.
First off you'll need to rename one of the columns to be swapped and add a column to hold the new value:
alter table table1 change currency salary int;
alter table table1 add currency varchar(3) after salary;
then update each table independently:
update table1 t1, table2 t2
set t1.currency = t2.salary
where t1.user_id = t2.user_id;
update table1 t1, table2 t2
set t2.salary = t1.salary
where t1.user_id = t2.user_id;
and finally remove the extra column:
alter table table1 drop salary;
I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?
I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id
I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"
The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)
I'm stuck with the following problem.
I have a website with for example supermarket shopping items. People can search the website for items.
Now I want on the search result page at the top 2 items to be displayed that I have selected to be on offer. There can be lots more items on offer.
So for example, someone would search for shampoo, the query would display all the shampoo items in the database table but I want just 2 shampoo offer items at the top of the query. There could be 2 or more shampoo offers in the database table, then the other would just not be shown.
Example with names :
Table:
id name C D
----------------------------------
1 Jack 1 1
2 Joe 1 1
3 Dave 3 0
4 Sue 1 0
5 Mike 1 1
6 Steve 4 0
7 David 1 0
8 Susan 4 1
9 Marc 1 1
10 Ronald 4 1
11 Michael 4 1
EXAMPLE 1
Query :
WHERE C = 1 AND D = 1 (But only maximum of 2 'D' records, these 2 'D' records show at the top of the result)
Desired Query Result :
id name C D
----------------------------------
1 Jack 1 1
2 Joe 1 1
4 Sue 1 0
7 David 1 0
EXAMPLE 2
Query :
WHERE C = 4 AND D = 1 (But only maximum of 2 'D' records, these 2 'D' records show at the top of the result)
Desired Query Result :
id name C D
----------------------------------
8 Susan 4 1
10 Ronald 4 1
6 Steve 4 0
I hope this explains my goal what I'm trying to achieve.
Many thanks for any help or suggestions!
That's two queries that you can combine with UNION ALL:
select * from mytable where c = 1 and d = 1 limit 2
union all
select * from mytable where c = 1 and d = 0
order by d desc;
UPDATE: If you want to have the two rows chosen randomly, then order by RAND(). (Without an ORDER BY the rows are chosen arbitrarily, which means it's not guaranteed to get the same two rows picked again when re-running the query ‐ but it's quite likely.) As we need an ORDER BY for a partial query (the first query in the complete union-alled query), we must use parentheses, because otherwise only one ORDER BY would be allowed, namely for the complete query at the query's end.
(select * from mytable where c = 1 and d = 1 order by rand() limit 2)
union all
(select * from mytable where c = 1 and d = 0)
order by d desc;
SQL fiddle: http://sqlfiddle.com/#!9/ed53f6/3.
I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?
You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here