I have a table:
Table1:
Id value
1 10
2 20
3 30
4 40
In table 2:
Id
1
1
2
2
3
4
4
4
I want to get the results:
Table2:
Id value
1 10
1 10
2 20
2 20
3 30
4 40
4 40
4 40
I know add a column with following code:
ALTER table Table2 ADD value int(11)
How to fill the data??
I think these SQL useful to you.
UPDATE Table2 LEFT JOIN Table1 ON Table2.id = Table1.id
SET Table2.value = Table1.value
Thank you.
you need to first ALTER the table and then fill the values using JOIN command as:
ALTER table Table2 ADD value int(11);
Then insert / fill the values as:
UPDATE Table2 INNER JOIN Table1
ON Table2.id = Table1.id
SET Table2.value = Table1.value
You could run this update command juste after tours.
UPDATE Table2 SET value = (SELECT value FROM Table1 WHERE Table2.Id = Table1.Id)
Related
i need some help to get a fast update on my table in MySQL
Table 1
id | value
1 0
2 0
3 0 ...
Table 2
t1_id | t2_id
1 2
1 3
3 5 ...
Have about 150,000 rows in table 1, and about 1,3 million in table 2. I need set t1.value = 1 when t1.id exists in table 2.
update table1 t1, table2 t2
set value = 1
where t1.id = t2.id;
Without some distinct parameter, it will do many times for each id, making it slow to update all t1 rows.
Any help would be gladly accepted.
what about:
UPDATE t1
SET t1.value = 1
FROM table_t1 t1
WHERE EXISTS (SELECT 1
FROM table_t2 t2
WHERE t2.id = t1.id
)
what about:
update table1
set value=1
from table2
where table1.id=table2.t1_id
I want get full column from one table and select sum from another table that have same id.
Eg:
table1 table2
id target id target achived
1 40 1 20
2 50 2 25
3 66
4 80
and i want to select all from table 1 and fill achived target results on it.
Eg:
id target target achived
1 40 20
2 50 25
3 66 0
4 80 0
how can i do that using mysql
Use this query:
select table1.*, case when table2.target_achieved is null
then 0
else table2.target_achieved
end as target_achieved
from table1 left join table2
on table1.id = table2.id
order by table1.id
Below query results in 0 instead of NULL
SELECT t1.id, target, COALESCE(target_achieved, 0) AS target_achieved
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.id;
I need to get all the records where table1.user is in table2.userid and table2.country = 8 and table2.status = 1
This is the sample data in my database
table1
id user
-- ----
1 12
2 23
3 34
4 32
5 85
6 38
table2
id userid country_id status
-- ---- ----- ----
1 12 5 1
2 12 8 1
3 85 8 1
4 38 8 0
5 38 7 1
6 23 8 1
7 23 4 1
in this case I should only get the id #3 in table2
Inner join will do the job taking only record with match in the two tables.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 on t1.user = t2.userid
WHERE t2.country=8 and t2.status=1
You mean
"get all the records where table1.user is in table2.userid
with table2.country = 8 and table2.status = 1 ?"
If so, then:
Select * from table1 t1
Where Exists(Select * from table2
Where userId = t1.user
and country = 8
and status = 1)
Well to accomplish that is quite simple, you just need to make use of an inner join. An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
In your case, the INNER JOIN will return all rows from table2 where the join condition is met.
For a more detailed explanation and examples just visit this link: http://www.w3schools.com/sql/sql_join.asp
based on your response to Adam's approach "
I tried this but it didn't work, yes it gets the records but the id #2 in table2 is also included. what I need to get is the id #3 in table2. Thanks :) ". I changed my SQL query to:
SELECT
*
FROM
table1
INNER JOIN
table2
ON
table1.user = table2.userid
WHERE
table2.userid = 85
I assumed you only want the data of that particular userid in table2
I have 2 tables in Mysql. I need to join them somehow to get one value from second table into the first one.
TABLE 1
Day EmployeeId Total EmployeeName
1 2 20 Josh
1 1 20 Mike
2 2 5 Josh
2 1 10 Mike
3 3 5 Eric
TABLE 2
Day EmployeeId Max_Total
1 2 40
1 1 40
2 2 5
2 1 15
I need to get something like TABLE 3
Day EmployeeId Total EmployeeName Max_Total
1 2 20 Josh 40
1 1 20 Mike 40
2 2 5 Josh 5
2 1 10 Mike 15
3 3 5 Eric null
So this Max_Total column needs to be somehow created and populated.
This Day_EmployedId combination is unique in both tables and that should be used somehow to extract values from 2nd table and add it to the first one.
Sometimes first table can have more values, sometimes the second one, but the first one will always be the one that needs to be manipulated/added to.
Any hint will be appreciated. Thanks
You are looking for a left join on two fields:
select t1.*, t2.max_total
from table1 t1 left join
table2 t2
on t1.day = t2.day and t1.employeeid = t2.employeeid;
I would not recommend actually updating table1. You can generate the data as you need it. However, in order for an update to work, you need to add a column to the table first, and then update it.
You need to separate your tasks.
Alter your Table1 to add the column Max_Total
Write UPDATE query to update your Max_Total in your Table1.
Query:
UPDATE t1.Max_Total = t2.Max_Total
SET t1.
FROM Table1 t1
JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
If you are only concerned about getting a combined result set
SELECT t1.Day, t1.EmployeeId, t1.Total, t1.EmployeeName, t2.Max_Total
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
For more information on LEFT JOIN, you can study this tutorial.
I have a table in my MySQL database that I need to remove the duplicates from.
My table looks something like this:
unique_id value frequency value_type publication_date
1 6.5 1 2 2014-12-31
2 7.5 3 5 2014-06-04
3 6.5 1 2 2015-07-13
4 8.0 4 3 2010-12-31
Rows 1 and 3 are duplicates except for the publication_date. I need to remove these duplicates but keep the row with the max publication date so for this example I would want to remove row 1 and keep row 3.
So far I've tried this but it's giving me too many results on my test table:
SELECT t.* FROM
(SELECT MAX(publication_date) AS most_recent_date
FROM table_1
GROUP BY `value`,frequency,value_type
) t1
JOIN table_1 t
ON t.publication_date = t1.most_recent_date;
Any help would be appreciated.
Thanks.
You can use JOIN to remove the duplicates as
delete t1 from table_1 t1
join table_1 t2 on t1.value = t2.value
and t1.frequency= t2.frequency
and t1.value_type = t2.value_type
and t1.unique_id <> t2.unique_id
and t1.publication_date < t2.publication_date ;