mysql how to join to table - mysql

structure table1:
tb1_id tb1_name
1 a
2 b
3 c
structure for table2:
id name tb1_id
1 a ?
2 b ?
3 c ?
4 a ?
5 a ?
6 b ?
Now lets guess here i have more then 10000 records in table2 and like 1000 records in table1 and i dont know the tb1_id here which i want to update table2 and set tb1_id as it is on table1.
for example it should look like(table2):
id name tb1_id
1 a 1
2 b 2
3 c 3
4 a 1
5 a 1
6 b 2
regards

For MySQL,
update table2 a
inner join table1 b on
a.id=b.tb1_id
set a.tb1_id=b.tb1_id
For SQL Server,
update a
set a.tb1_id=b.tb1_id
from table2 a
inner join table1 b on
a.id=b.tb1_id

Use Update from Join syntax. Try this.
UPDATE table2 AS a
JOIN table1 AS b
ON a.name = b.tb1_name
SET a.tb1_id = b.tb1_id

Related

How to form a join query in mysql to filter records based on some status condition

Consider I have table t1
id
Another header
1
row1
2
row2
3
row3
4
row4
and another table t2
id
t1_id
status
1
1
PG
2
2
S
3
1
CG
4
1
S
5
3
CG
t1 has one2many relationship with t2.
The t1_id has multiple entries in t2 table because t2's status sequence goes on like
PG > CG >S.
So it can have separate entry for each status it passes through.
Once it reaches status S we should not select that t1_id even if it has any of the previous statuses like PG or CG
I want to do left join like t1 left join t2 based on t1 id
And another condition like t2's status should not have S.
So the expected result should be
t1_id
status
3
CG
4
null
How can I achieve this ? Thanks in advance
First retrieve t1_id where status 'S' exists and ignore those t1_id from query if any other status info for that t1_id is existed in the table.
-- MySQL (v5.7)
SELECT t.id t1_id, p.status
FROM t1 t
LEFT JOIN t2 p
ON t.id = p.t1_id
WHERE NOT EXISTS (SELECT 1
FROM t2
WHERE t1_id = t.id AND status = 'S');
Please check from url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=2a38e8a8696b68c15f8e95a28f244c86

Vlookup in SQl table

table1
Agent id
A 1
B 2
C 3
D 4
E 5
table2
Agent id
C 3
D 4
output:
Agent id
A 1
B 2
E 3
Do you want not exists?
select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.agent = t1.agent and t2.id = t1.id)
This brings records of table1 for which there is no corresponding record in table2. I assumed that you want to match on both columns: you might to review that, and adapt the where clause of the subquery accordingly.

MYSQL: How to JOIN two tables on the same query referencing the same table twice

I have two tables. I'm trying to JOIN the sample two tables below with table 1 referencing Table 2 twice. For example if I look at Table 1: Group 2 and Members 7, it should look up the ID in Table 2 and give me an output of:
Group Members Name Name
2 7 Blue Dog
Table 1
Group Members
2 7
3 8
5 10
Table 2
ID Name
1 Green
2 Blue
3 Yellow
4 Orange
5 Red
6 Elephant
7 Dog
8 Cat
9 Chicken
10 Mouse
Any Advice? Thanks
SELECT
Table_1.*,
g.Name,
m.Name
FROM
Table_1
INNER JOIN Table_2 AS g ON Table_1.Group=g.ID
INNER JOIN Table_2 AS m ON Table_1.Group=m.ID
WHERE
Table_1.Group=2
AND Table_1.Member=7
Join with Table 2 twice on different columns.
SELECT t1.*, t2.name AS group_name, t3.name AS member_name
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.group = t2.id
JOIN Table2 AS t3 ON t1.members = t3.id
Hard to tell exactly what you need from that description but aliasing the tables may be what you need. It works like this:
SELECT t1.x, t2_1.y, t2_2.z
FROM table1 AS t1
JOIN table2 AS t2_1 ON t1.whatever = t2_1.whatever
JOIN table2 AS t2_2 ON t1.whatever = t2_2.whatever
...

How do I update the values of a column in a database using another mapping table? (MySQL)

Imagine I have the following tables
Table 1
id question_id tag_id
1 1 10
2 5 8
Table 2 (Mapping table)
old_tag_id new_tag_id
8 89
9 90
10 91
Result That I'm trying to achieve:
Table 1
id question_id tag_id
1 1 91
2 5 89
.
.
(that is, Update the whole table in using a single query, since I have a table consisting of millions of rows)
I tried using subqueries in SQL but I can't seem to handle when the subqueries seem to return more than one row.
Thanks In Advance
You can use an INNER JOIN query
UPDATE table1 a
INNER JOIN table2 b
ON a.tag_id = b.old_tag_id
SET a.tag_id = b.new_tag_id
update table1 t1
join mapping_table t2 on t1.tag_id = t2.old_tag_id
set t1.tag_id = t2.new_tag_id
Can you try the below query.
UPDATE Table1 t1
SET t1.tag_id = (SELECT t2.new_tag_id
FROM Table2 t2
WHERE t1.tag_id = t2.old_tag_id)

code igniter - one to many child join?

Hi Guys I have two tables table B has a many to 1 relationship with table A
TableA TableB
id name id value
1 basketA 1 10
2 basketB 1 5
1 7
2 7
2 3
etc..
now
$query = $this->db->get('TableA');
return $query->result_array();
returns the A fields obviously but how can I do a join so it will return A-Field along with the sum of the B-Items for that field?
eg. in the result array
BasketA 22
BasketB 10
Thanks in advance!
try this:
select A.id,A.name,SUM(B.value)
from TableA A join tableB B
on A.id=B.id
group by A.id,A.name
This should work:
SELECT A.name, SUM(B.value) AS sum_value
FROM TableA A
INNER JOIN tableB B
ON A.id=B.id
GROUP BY A.id;