Updating rows with value of another column in a different table - mysql

There are two same structured tables i.e. One & Two. I want to update one column with values of another table's same column.
Have a look at this:
Table One
id name value
1 a 11
2 b 12
3 c 13
Table Two
id name value
1 c 11
2 d 12
3 e 13
I want to update one.name with the values of two.name. How do I do that?

Use a JOIN in the UPDATE to relate the two tables.
UPDATE One
JOIN Two ON One.value = Two.value
SET One.name = Two.name
If you need to use LIMIT, you have to use a subquery:
UPDATE One
JOIN (SELECT *
FROM Two
LIMIT 100) AS Two
ON One.value = Two.value
SET One.name = Two.name

Related

How to join table with 2 columns

I want to join 2 columns base on this :
A.Contactnumber and the 2 column B.old_contact, B.recent_contact
I want to retain the old contact and at the same time include recent if they're not alike
select a.*, b.old_contact, b.recent_contact
from table a
left join table b
on a.contactnumber = b.old_contact
SELECT a.contactnumber, b.old_contact, b.recent_contact
FROM a INNER JOIN b ON a.contactnumber=b.old_contact
WHERE b.old_contact!=b.recent_contact;
as the result you will get the values which are present in table "a" and in table "b" and which have different values in columns "old_contact" and "recent_contact" in table "b". Is that what you need?

Update values in the database

I want to update the description in table b. In table a I have the newest data. I want to join on all the numbers that are available in table b and update the description. Only the numbers that are available in table b must be updated!
I have this query:
select * from b
left join a.prod_nr = b.prod_nr
But how can i do an update to the fields? And how is the performance because there are updated 8000 rows.
I have table a
prod_nr description
1 test
2 things
3 pc
table b
prod_nr description
1 nothing
3 monitor
output after query in table b:
prod_nr description
1 test
3 pc
update b
join a on a.prod_nr = b.prod_nr
set b.description = a.description

COUNT(*) return wrong number

It seems I don't get it something. Please consider this query
SELECT COUNT(*) AS `numrows`
FROM (`exp_channel_titles` ch)
JOIN `exp_channel_data` cd ON `cd`.`entry_id`=`ch`.`entry_id`
LEFT JOIN `exp_matrix_data` md ON `md`.`entry_id`=`ch`.`entry_id` and field_id = 14
LEFT JOIN `exp_assessment_users` au ON `au`.`entry_id`=`ch`.`entry_id`
WHERE ch.channel_id = 4 GROUP BY `ch`.`entry_id`
it returns 2
but if I change it to
SELECT *
FROM (`exp_channel_titles` ch)
JOIN `exp_channel_data` cd ON `cd`.`entry_id`=`ch`.`entry_id`
LEFT JOIN `exp_matrix_data` md ON `md`.`entry_id`=`ch`.`entry_id` and field_id = 14
LEFT JOIN `exp_assessment_users` au ON `au`.`entry_id`=`ch`.`entry_id`
WHERE ch.channel_id = 4 GROUP BY `ch`.`entry_id`
result is 1 row only. How so?
You're grouping, which means internally matching rows are collapsed into a single entity. e.g. consider a fake table like this:
field
-----
a
a
Yes, a one field table, with two records, both of which have the value a in them.
SELECT *
FROM table
GROUP BY field
group by will find all fields which have the same value, and collapse them down into a SINGLE record, so your two records of a become one row in the result set, and you end up with
field
-----
a
But doing
SELECT count(*)
FROM table
GROUP BY field
changes things. Now the DB will literally count how many records were collapsed down into the single row of result set. So you still get a SINGLE row in the result set, which contains a count of how many rows were collapsed by the group by:
count(*)
--------
2
One row, with a value of 2, because there were two rows with a.
Now if you had a table with more records:
field
-----
a
a
b
c
c
c
You would get:
SELECT * ... GROUP BY field
field
-----
a
b
c
SELECT count(*), field ... GROUP BY field
count(*) field
----------------
2 a
1 b
3 c
again, 3 rows of results, but note how the count represents how many of each grouped field there are in the original table.

Joining tables without getting the unmatched records

I have the below two tables (one & two) and need the output as in the third table.
ONE
ID TAG
1 A
2 B
3 c
TWO
ID TAG
1 A
2 Z
OUTPUT
ID TAG
1 A
3 C
Conditions -
1. Need the values for which the 'TAG' matches
2. Need the values from 'ONE' which are not available in the 'TWO' table
3. Do no need the values for which the 'TAG' does not match
Can this be done in a single SQL query?
A LEFT JOIN keeps the unmatched result from the first table:
SELECT one.id AS id, one.tag AS tag
FROM one LEFT JOIN two ON one.id = two.id
WHERE one.tag = two.tag OR two.tag IS NULL;
The first condition one.tag = two.tag gets the matching result; the second two.tag IS NULL gets what are available in table one but not two.
Checkout the demo here. Let me know if it works.
select a.ID, a.TAG
from ONE a LEFT OUTER JOIN TWO b
ON (a.ID = b.ID
and a.TAG = b.TAG)

Getting value of Mapping table IDs when value is from same column

Im really struggling to get my head around what should be simple,
I have two tables, one contains records the other is a mapping table.
records
ID Title Description
1 record 1 desc 1
2 record 2 desc 2
3 record 3 desc 3
4 record 4 desc 4
mapping table
ID1 ID2
1 3
2 4
What I want to do is get the two titles of each row in the mapping table. So the above would output
record 1 record 3
record 2 record 4
Im missing something really obvious, trying multiple joins results in errors trying to link the same table twice.
The following returns NUll
SELECT records.title FROM mapping
LEFT JOIN records
ON mapping.ID1 = records.id
AND mapping.ID2 = records.id
try this one: (UNTESTED)
SELECT b.Title as TitleA,
c.Title as TitleB
FROM mapping a
INNER JOIN records b
on a.ID1 = b.ID
INNER JOIN records c
on a.ID2 = c.ID