Updating Table Rows with another table data - mysql

I have two tables I am trying to update table A data with table B. Table A (ids) has 100 rows (based on a condition) and I want to replace those ids with table B(ids).
Table A
abc
def
klm
ijk
Table B
abc1234
pknm
lokimh
2546njnh
can you please provide me the SQL to replace these ids form table B to table A
there are 100 rows which i need to update from table B to table A.
Cursor trans
select id
from table B
where rownnum <=100
cursor user
select id
from table A

update tab_a set colname =
(select colname from tab_b where -- condition, use the condition you are referring to )

Related

Fill Column of SQL Table

I'm trying to fill a certain column of a SQL table with data from another table. I have a column named "size" in my table which should return the number of rows in the 2nd table where the id of both rows is the same. Is there a way to populate a SQL column based on a certain command? I would love to be able to fill the column based on this command:
SELECT count(*)
FROM second_table
WHERE id = "row_id";
Here is a sample database with the two tables:
Table 1
Name
id
tiger
1
lion
2
gazelle
1
Here is the desired output for Table 2:
id
Number of Animals
1
2
2
1
I am trying to fill the Number of Animals column but do it automatically and dynamically when another row is added or deleted to Table 1, which is why I want the Select count(*) SQL statement as the code for the column.
One method is a correlated subquery:
update table1 t1
set size = (select count(*)
from table2 t2
where t2.id = t1.id
);
If you need to do this dynamically (as data is inserted), then you would need to use a trigger. However, I would suggest that you calculate the value as needed, unless there is a specific reason why you need to store it.
I guess you need something like this:
CREATE TRIGGER UpdateAnimalCountTable2
AFTER INSERT ON `Table1` FOR EACH ROW
begin
DECLARE NewCount int;
SELECT count(1)
INTO #NewCount
FROM Table1
WHERE Table1.id= NEW.id;
UPDATE Table2
SET NoOfAnimals = #NewCount
WHERE id = NEW.id;
END;
Above is the trigger which will be executed after every insert in Table1 and will update the count in Table 2 for ID which just got inserted in Table1.

Comparing table B to table A?

select A.`Product#`,
a. ` Email Address`
FROM
Table a A, `Table b B
where A.`Product#`<> B.`Product#`
I am trying to compare if Table B's product# exists in Table A or not, this query does not give 100 % result.
Please tell me what is wrong in this query.
You should use a NOT EXISTS query
select *
FROM
Table b B
where not exists ( select 1 from table a A where A.`Product#`= B.`Product#`)
This will show you all that exists in table B that does not exist in table A which is what I believe you were asking in your question. Can be reversed to show what's in table A that is not in table B.

updating records in table A from table B if column XYZ ( checksum ) changes in table B

I am having a table A and table B.
Table A is created from Table B ( and few other table join operation ).
Table A has all of its column which are subset of column in table B.
There is a column called as check_sum in table A and table B. This is basically a calculated column and if any column value changes then check_sum ( calculated value ) changes as well.
For example:
Table A ( schema ):
cust_id ( pk ), cust_name, cust_location, check_sum ... other columns ( no need to worry about them )
Table B ( schema ) :
cust_id ( pk ), cust_name, cust_location, check_sum
Initially table B and A have entries like below:
Table A: ( sample record )
1, John, USA, abc222abc, ... other columns
Table B: ( sample record )
1, John, USA, abc222abc
Now lets say John changes his country location to UK, then corresponding entry in TABLE A looks like this
Table A: ( sample record )
1, John, UK, checkSumChanged, ... other columns
Now i need to update my table B accordingly, so that instead of location of John as USA it should have it as UK.
Column checksum is helpful here, since its value changes in Table A if any column changes.
This is the part i am stuck at. Not able to update just "CHANGED" rows from Table A to Table B. I have following query below. It is updating all rows instead of just the changed rows.
Here is the query.
UPDATE tableB
SET
tableB.cust_name = tableA.cust_name,
tableB.cust_location = tableA.cust_location,
tableB.check_sum = tableA.check_sum
FROM
tableA inner join tableB
on tableA.cust_id = tableB.cust_id
and tableA.check_sum != tableB.check_sum
Any ideas or suggestion how can i correct my query to just update the changed record.
Thanks in advance!!!
Do it without a join:
update B
SET cust_name = a.cust_name, checksum = a.checksum, cust_location = a.cust_location
FROM a
WHERE a.custid = b.custid AND a.checksum != b.checksum;

how to remove duplicate values from MySQL table

i am looking for the query, deletes the all duplicate values.
Example Table:
1 ABC
2 BBB
3 DAC
4 ABC
5 AAA
6 ABC
output required
1 ABC
2 BBB
3 DAC
5 AAA
thanks for your help, i Google it can't find exact solution.
If you want to do an actual DELETE operation of the duplicate values (while retaining the values having the lowest id), you can do it with the multiple table DELETE syntax:
DELETE a FROM tbl a
LEFT JOIN
(
SELECT MIN(id) AS id, name
FROM tbl
GROUP BY name
) b ON a.id = b.id AND a.name = b.name
WHERE b.id IS NULL
See a demonstration of the DELETE operation
See #fvu answer here : https://stackoverflow.com/a/11249235/1166147
You can create a unique index that will remove duplicates and prevent future dupes all at once (MySQL 5.1 or higher):
ALTER IGNORE TABLE 'YOURTABLE'
ADD UNIQUE INDEX somefancynamefortheindex (Col1ID, Col2)
Assuming Tab is the name of your table containing duplicates, create a temporary table Tab_TMP with the same structure of Tab.
-- assuming `Tab` has same schema
CREATE TABLE Tab_TMP (
id INT(2) PRIMARY KEY,
name VARCHAR(8)
);
Fill Table_TMP with all Table entries.
INSERT INTO Tab_TMP(id, name) SELECT id, name FROM Tab;
Delete entries from Table.
DELETE FROM Tab;
Insert in Table entries from Table_TMP using SELECT DISTINCT.
INSERT INTO Tab(name) SELECT DISTINCT name FROM Tab_TMP;
load distinct data in a file and delete or drop your table
SELECT DISTINCT col1,col2,col3,..coln FROM table name INTO OUTFILE 'yourFilePathWIthFileName'
eg:
SELECT DISTINCT username,usernumber,usersalary,dateOFJoining,timeofJoining,datetimeOfJoining FROM asdf INTO OUTFILE 'C:/Users/ahmed.m/Desktop/modify.txt';
create your table structure laod data back from file
LOAD DATA INFILE 'yourFilePathWIthFileName' INTO TABLE tableName
eg:
LOAD DATA INFILE 'C:/Users/ahmed.m/Desktop/modify.txt' INTO TABLE asdf

Creating a table on the basis of a field

I have a MySql table which has about 100k rows. there is one field say id which contains numbers from 1-35. all these records fall in this range of id i.e. all these records have value of id column between 1-35.
Now i want to create another table which will have one row of each id. i.e the new table should have 35 rows only.
How to go about it ?
create table new_table (id int);
insert into new_table
select distinct id from big_table;
Edit:
You can create the new_table by outputting the big_table create script and changing the name.
SHOW CREATE TABLE big_table;
/* modify the name of the output and execute */
insert into new_table
select * from big_table group by id
You have a table with 100.000 rows, and you want a new table with 35 rows. What values do you want for the remaining columns?
If the answer is: doesn't matter, this works:
CREATE TABLE newTable
SELECT * FROM yourTable
GROUP BY ID;
If you only want the IDs,
CREATE TABLE newTable
SELECT DISTINCT ID FROM yourTable;
You can copy data from one table to another even difference database(Schema) as following
INSERT INTO [DestDatabase].[DestTablName]
SELECT [ColumnName] FROM [SourceDatabase].[SourceTablName];
So, you can use two way:
1:
INSERT INTO tbl_New
SELECT DISTINCT id from tbl_Original;
2:
INSERT INTO tbl_New
SELECT id from tbl_Original GROUP BY id;