I need to update the ManagerId as I have following table structure
userid EmpAccId ManagerAccId ManagerId
1 X Y
2 Y Z
3 Z X
I need to update the above table based on EmpAccId and ManagerAccId as for userId 1 The managerId should be 2 as EmpAccId for userid 1 is X and ManagerAccId is Y and Y's userid is 2 so for userid 1 MangerId should be 2.
so the resultant table should be
userId EmpAccId ManagerAccId ManagerId
1 X Y 2
2 Y Z 3
3 Z X 1
I need a single dynamic query in sql.
you can use a left join to achieve this :
UPDATE mytable as TB1
LEFT JOIN mytable as TB2
ON TB1.ManagerAccId = TB2.EmpAccId
SET TB1.ManagerId= TB2.userId
try this
UPDATE tbl_a t1
INNER JOIN tbl_b t2
ON t1.EMPAccId = t2.EMPAccId
AND t1.ManagerAccId = t2.ManagerAccId
SET t1.ManagerID = t2.ManagerID;
This will also help you
update table t1 left join table t2 on t1.EmpAccId=t2.ManagerAccId set t2.ManagerId=t1.userId;
Related
I have two tables and I want a query to show records from one table that match the second table or doesn't exists at all. In other words if the record exists in table B and does not match the condition don't show it.
TABLE A TABLE B
ID VAL ID AID BVAL
--------- ----------------
1 v1 1 2 B1
2 v2 2 3 B2
3 v3
I've tried with this query:
SELECT ta.id, ta.val, tb.bval
FROM table_a ta
LEFT JOIN table_b tb ON ta.id = tb.AID AND tb.BVAL = 'B1'
the goal is to get ONLY this rows:
ID VAL BVAL
------------------
1 v1 NULL
2 v2 B1
But obviously with this query I get all Table A.
Thanks for your time!
I have found a solution, I post it in case someone else is having the same issue.
SELECT ta.id, ta.val, tb.bval
FROM table_a ta
LEFT JOIN table_b tb ON ta.id = tb.AID
GROUP BY ta.id, ta.val, tb.bval
HAVING tb.BVAL = 'B1' OR tb.BVAL IS NULL
How do i select from table 2 and show inside table 1,sql correct statement ?
Example:
Table 1 row
A B C D
Table 2 columns
x
y
z
How do i
SELECT * FROM Users
as single row [[A] [B] [C] [D] [x y z]] ?
if i do SELECT * FROM table1 WHERE id = "k", i get 1 result
if i do SELECT * FROM table2 WHERE id = "k",i get 3 results
i want all 3 results from table 2 to show in table1 in result 1
Another example :
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table1.id = '" + k + "'"
i get this : [[result1 table 1],[result1 table2]],[result1 table 1],[result2 table2]]] incorrect for me
i want [[result1 table 1],[result1 table2]],[result2 table2]]]
subquery NOT as solution as it limits me to 1 row
SELECT *
FROM `table_1`
LEFT
JOIN `table_2`
ON `table_1`.id=`table_2`.id_table1
WHERE id_table1
Obviously the id_table1 has to be the id that matches both tables in a relation of 1:N
I hope you find this usefull.
I have two tables, lets called them A and B. Here is what they look like
Table A:
ID BCODE
1 A1
2 B1
3 C1
4 D1
5 F1
Table B:
X Y IDX IDY
A1 D1
D1 F1
C1 B1
Table B has columns 'X' and 'Y' that have values that are found in Table A's column BCODE.
I want to insert the ID from Table A for each joined X,Y column with Table A's BCODE. Each matching BCODE for X goes to IDX. Each matching BCODE for Y goes to IDY. This is what it should look like
Table B:
X Y IDX IDY
A1 D1 1 4
D1 F1 4 5
C1 B1 3 2
I have a table A and B with about 50 million rows in both Table A and B
Does anyone know how I can do this ? I tried using INNER JOINS but it didnt populate all the columns. Thanks
you need a simple join with the Update command:
in MS SQL Server:
Update tableB
set
tableB.IDX=a.ID,
tableB.IDY=aa.ID
from
tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE
in MySQL:
Update tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE
set
tableB.IDX=a.ID,
tableB.IDY=aa.ID
(Edit: as question is about MySQL I just create The DEMO for it)
in Oracle PL/SQL:
Update (select tableB.IDX idx, tableB.IDY idy, a.ID ida, aa.ID idaa
from tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE)
set
idx=ida,
idy=idaa
UPDATE b
SET idx =
(SELECT id
FROM a
WHERE bcode = b.x),
idy =
(SELECT id
FROM a
WHERE bcode = b.y);
I am using MySQL 5.6.17.
I have a self-referencing table TableA with columns id (PK), title (varchar), type (varchar), parent_id (FK that refers to id of the same table).
The sample data is as below :
id title type parent_id
1 abc G NULL
2 def G NULL
3 xyz G NULL
4 pqr G NULL
5 abc T NULL
6 def T NULL
7 xyz T NULL
8 pqr T NULL
Now, I want each record having type='G' should become the child of the record with type='T' having the same title.
So the resultant table data should be :
id title type parent_id
1 abc G 5
2 def G 6
3 xyz G 7
4 pqr G 8
5 abc T NULL
6 def T NULL
7 xyz T NULL
8 pqr T NULL
I've tried query below :
UPDATE TableA
SET parent_id = (SELECT id FROM ( SELECT id FROM TableA WHERE TYPE='T' ) d)
WHERE TYPE='G';
But it returns
Error Code: 1242
Subquery returns more than 1 row
I also have tried :
UPDATE TableA t1
SET t1.parent_id = t2.newlocalid
INNER JOIN (
SELECT title, id AS newlocalid
FROM TableA t2
WHERE TYPE='T'
) t2 ON t1.title = t2.title
WHERE t1.type='G'
But it also returns the error in Syntax.
Can anyone help me to achieve it?
UPDATE TABLEA a
JOIN TABLEA b ON a.title = b.title and a.type='G'and b.type='T'
SET a.parent_id = b.id
This should work:
SELECT title, id AS newlocalid
INTO #t2
FROM TableA
WHERE TYPE='T'
UPDATE t1
SET t1.parent_id = t2.newlocalid
FROM TableA as t1
INNER JOIN #t2 as t2
ON t1.title = t2.title
WHERE t1.type='G'
Try:
UPDATE TableA a
SET parent_id = (SELECT id FROM TableA ref WHERE TYPE='T' AND ref.title=a.title)
WHERE TYPE='G';
to update the table column first you have to identify which value to set. you can not use multiple values to update the single column in same statement.
change your sql to something like this:
UPDATE TableA
SET parent_id = (SELECT id FROM TableA WHERE TYPE='T' limit 0,1)// i mean make sure that it is returning single record not multiple.or better add some more where condition to get a single and required record without using limit
WHERE TYPE='G';
or to some specific condition like this:
UPDATE TableA
SET parent_id = (SELECT id FROM TableA aa WHERE TYPE='T' and aa.type=TableA.type)
WHERE TYPE='G';
Lets say I have a column named source in a table x. Individual entries can be like;
Id c1 c2 source ...
1 a b something
2 b a something
3 a b somethingelse
4 c a somethingelse
5 a b something
6 b c something
How can I delete entries with less than 3 same elements in source? For example since source value somethingelse appears 2 times, I need all entries that have somethingelse removed.
DELETE a
FROM tableName a
INNER JOIN
(
SELECT source
FROM tableName
GROUP BY SOURCE
HAVING COUNT(*) < 3
) b ON a.source = b.source
SQLFiddle Demo
One more thing to do for faster performance, add an INDEX to column SOURCE.
Roughly something like this would do the job
DELETE FROM TABLE_T1 WHERE ID IN (
SELECT ID FROM TABLE_T1 GROUP BY SOURCE HAVING COUNT(*) < 3
)
DELETE id
FROM yourtable a
JOIN (
SELECT *
FROM yourtable
GROUP BY
source
HAVING COUNT(*) > 3
) b
ON a.id = b.id
DELETE FROM x WHERE id IN
( SELECT id FROM
( SELECT id, COUNT(source) AS n
FROM x GROUP BY source
HAVING n < 3
)
)