SQL Merging Tables Based On Multiple Matching Column Values - mysql

Trying to merge two game servers' tables that have this structure:
map | authid | name | time | date | ...
I would like to replace a row only if the time value of table2 is less than that of table1 AND ONLY if the map and authid values are BOTH the same. If the time values in table2 are greater, then the row from the current table (table1) should be kept untouched. Otherwise (on different map or authid values), the row from table2 should simply be appended to table1.

My way would be to
1st: create a view for replacing rows and another with the correct result set that should be appended like
--view for update
create view ChangeTable1
as
select table2.map, table2.authid,table2.name, table2.date, table1.map as t1map, table1.authid as t1authid...from table1 to inner join table2 on table1.map=table2.map and table1.athid=table2.autid
where table1.time>table2.time
-- view for appending
create view Add2Table1
select table2.map, table2.authid,table2.name, table2.date... from table2 where concat(table2.map, table2.authid) not in (select concat(table1.map, table1.authid) from table1)
-- update statement based on first view ChangeTable1
update ChangeTable1 set t1date=date, t1somevalue=somevalue......
-- insert Statement based an second view Add2Table1
insert into table 1 (map, authid, name, time, date, .... as select map, authid, name, time, date,... from Add2Table1
I hope this helps. I mostly do MS SQL, so there might be some syntax issues that need translation to MYSQL, Nils
If you need a permanent process doing this, you might consider putting this in a stored procedure

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.

Insert records for each selected record

I have two tables, Table1 contains a list of records with columns super_id, user_name and job_type
Table2 contains a 3 columns as well super_id, view and time
Using a select query with criteria on table I would like to create one record per super_id in Table2
Meaning if the select query was SELECT super_id FROM Table1 WHERE job_type = “Instructor”
RF34323 through RF34328 would appear would each be inserted once into Table2 where the View column is always View1 and time is the current date.
How can an Select Insert query like this written?
The following is an example of the 2 tables:
Is this what you want?
insert into table2 (super_id, `view`, `time`)
select super_id, 'view1', now()
from table1
where job_type = 'Instructor';
Note that view and time are very BAD choices for column names, because they are keywords in SQL.
If you want to create table2, then use create table table2 as instead of insert.
Also, you can default the time column to the insertion time, if you set up the table properly.

How to 'Select Specific data, copy only certain columns and insert it into a different table?'

Currently, I find it time consuming to copy specific columns and values from Table 1 and paste the values onto Table 2 manually.
I have two tables ( In the same database ) like this:
How can I save time by doing the following? -
Grab specific ID from Table 1, in this case 1 and 2.
and copy just the Pcode & Desc values onto Table 2?
This is the end result I want to achieve (screenshot below)
The ID will be new, because its a new record. So technically I am updating Table 2 with new values that I have copied from Table 1
Every column is varchar type column expect the Id's
Also, I am using MySql Workbench.
This should do the trick;
INSERT INTO table2 (PCode, Desc)
SELECT Pcode, Desc
FROM table1
WHERE table1.id = 1 or table1.id = 2

Copy row from one MySQL DB to another

How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.

Store records in a new table created by a query in mysql

I have two tables ,location and locationdata. I want to query data from both the tables using join and to store the result in a new table(locationCreatedNew) which is not already present in the MySQL.Can I do this in MySQL?
SELECT location.id,locationdata.name INTO locationCreatedNew FROM
location RIGHT JOIN locationdata ON
location.id=locationdata.location_location_id;
Your sample code in OP is syntax in SQL Server, the counter part of that in MySQL is something like:
CREATE TABLE locationCreatedNew
SELECT * FROM location RIGHT JOIN locationdata
ON location.id=locationdata.location_location_id;
Referance: CREATE TABLE ... SELECT
For CREATE TABLE ... SELECT, the destination table does not preserve information about whether columns in the selected-from table are generated columns. The SELECT part of the statement cannot assign values to generated columns in the destination table.
Some conversion of data types might occur. For example, the AUTO_INCREMENT attribute is not preserved, and VARCHAR columns can become CHAR columns. Retrained attributes are NULL (or NOT NULL) and, for those columns that have them, CHARACTER SET, COLLATION, COMMENT, and the DEFAULT clause.
When creating a table with CREATE TABLE ... SELECT, make sure to alias any function calls or expressions in the query. If you do not, the CREATE statement might fail or result in undesirable column names.
CREATE TABLE newTbl
SELECT tbl1.clm, COUNT(tbl2.tbl1_id) AS number_of_recs_tbl2
FROM tbl1 LEFT JOIN tbl2 ON tbl1.id = tbl2.tbl1_id
GROUP BY tbl1.id;
NOTE: newTbl is the name of the new table you want to create. You can use SELECT * FROM othertable which is the query that returns the data the table should be created from.
You can also explicitly specify the data type for a column in the created table:
CREATE TABLE foo (a TINYINT NOT NULL) SELECT b+1 AS a FROM bar;
For CREATE TABLE ... SELECT, if IF NOT EXISTS is given and the target table exists, nothing is inserted into the destination table, and the statement is not logged.
To ensure that the binary log can be used to re-create the original tables, MySQL does not permit concurrent inserts during CREATE TABLE ... SELECT.
You cannot use FOR UPDATE as part of the SELECT in a statement such as CREATE TABLE new_table SELECT ... FROM old_table .... If you attempt to do so, the statement fails.
Please check it for more. Hope this help you.
Use Query like below.
create table new_tbl as
select col1, col2, col3 from old_tbl t1, old_tbl t2
where condition;