Merge a specific table column from one database to another - mysql

I have two SQL databases. One is an older backup of the other.
I would like to merge only 1 specific table from [user_database0] into [user_database1] using either ssh or inside phpMyAdmin.
That table that I want to restore from backup is called [prefix_table].
However, I don't want to restore all columns from that table [prefix_table], just the [comment] column.
One of my biggest concerns is that some of the rows from the [prefix_table] have been deleted and I DO NOT want to restore those deleted rows from the old database.
Here is an Example:
*- Merge table [prefix_table]from [user_database0]:
prefix_table
+---------------------------------------------------+
| id | name | comment | age | person_id |
+++++++++++++++++++++++++++++++++++++++++++++++++++++
| 1111 | name1 | old text 1 | 01 | 001 |
+------------+-------+------------+-----+-----------+
| 2222 | name2 | old text 2 | 02 | 002 |
+------------+-------+------------+-----+-----------+
| 3333 | name3 | old text 3 | 03 | 003 |
+------------+-------+------------+-----+-----------+
| 4444 | name4 | old text 4 | 04 | 004 |
+------------+-------+------------+-----+-----------+
*-Into table [prefix_table] in [user_database1] :
prefix_table
+-----------------------------------------------------+
| id | name | comment | age | person_id |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 1111 | namenew | new text 1 | 99 | 001 |
+------------+---------+------------+-----+-----------+
| 4444 | name4 | new text 4 | 04 | 004 |
+------------+---------+------------+-----+-----------+
| 5555 | name5 | text 1 | 05 | 005 |
+------------+---------+------------+-----+-----------+
| 6666 | name6 | text 2 | 06 | 006 |
+------------+---------+------------+-----+-----------+
*- Resulting database [user_database1]:
prefix_table
+-----------------------------------------------------+
| id | name | comment | age | person_id |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 1111 | namenew | old text 1 | 99 | 001 |
+------------+---------+------------+-----+-----------+
| 4444 | name4 | old text 4 | 04 | 004 |
+------------+---------+------------+-----+-----------+
| 5555 | name5 | text 1 | 05 | 005 |
+------------+---------+------------+-----+-----------+
| 6666 | name6 | text 2 | 06 | 006 |
+------------+---------+------------+-----+-----------+
So it basically has to check if the table [prefix_table] matches in both databases then overwrite the data in the [comment] column. Note that if other column data changes, it should leave it as is, only the [comment] should be updated.
In Summary (both databases are on same server in one phpMyAdmin account)
FROM: [user_database0].[prefix_table].[content]
TO: [user_database1].[prefix_table].[content]
IF: [id] column matches in both tables.
Here is a working version from the recommendations below:
UPDATE
[new_db_name].[table_name]
INNER JOIN
[old_db_name].[table_name]
ON
[new_db_name].[table_name].[column name] = [old_db_name].[table_name].[column name]
SET
[new_db_name].[table_name].[matching column name] = [new_db_name].[table_name].[matching column name]

The following will work. Here it is updating the new comment with the old comment for any matching IDs in both.
Edit: This should point you in the correct direction. I had your schema before, I misunderstood what you wrote - my bad.
UPDATE [new_table_name] SET comment = [old_table_name].onecomment
FROM [new_table_name]
INNER JOIN [old_table_name] ON [new_table_name].aboutme_id = [old_table_name].aboutme_id
Note: the above syntax is SQL Server because the question was at first erroneously tagged sql-server.

Related

MySQL - Is there a better way to structure one of my table to reduce the amount of columns?

I have a (3x3) table on my website which gives the users the option to unlock a car slot, purchase a car and upgrade the car level.
In total I end up with 9 slots, all 9 slots can have different values (car_unlocked (bool), car_level (int), car_type (int)).
my current table : (user_cars)
id (AUTO_INCR) | user_id | car_slot_unlocked01 | car_level01 | car_type01 | car_slot_unlocked02 | car_level02 | car_type02 | car_slot_unlocked03 | car_level03 | car_type03 | car_slot_unlocked04 | car_level04 | car_type04 …
And so on until 9. I end up with 29 columns. And then I retrieve the values with the user_id on my website. Values are unique to every user.
How would I go about reducing the columns amount, because if I was to add more rows (4x4) or (5x5) to my website table I would end up with alot more columns in my database.
Do I create different db table and join it if that is even an option?
Thank you in advance.
Maybe you can give an ID to the cars so you don't need the repeating columns... something like:
id | user_id | car_id | car_slow_unlocked | car_level | car_type
-----------------------------------------------------------------
1 | 001 | 01 | 1 | 3 | 3
2 | 001 | 02 | 0 | 5 | 2
3 | 001 | 03 | 0 | 5 | 3
4 | 001 | 04 | 1 | 6 | 1
5 | 001 | 05 | 1 | 1 | 1
6 | 001 | 06 | 0 | 5 | 2
7 | 001 | 07 | 0 | 5 | 3
8 | 001 | 08 | 1 | 6 | 1
9 | 001 | 09 | 1 | 1 | 1
10 | 002 | 01 | 1 | 5 | 3
11 | 002 | 02 | 0 | 3 | 1
12 | 002 | 03 | 1 | 1 | 1
... and so on
Seems like you need to have a think about your data model. I don't quite understand your business but start with identifying the entities and their attributes outside of any presentation concerns.
e.g.
CarInstance (ID, type, unlocked)
CarType(ID, CarID, Level)
User(ID, name)
UserToCar(userID,carID)
I doubt those are right but that's the kind of thing you need to be aiming for. You can then generate your table from that.

How to dynamically add new records from one table into another in PowerPivot?

I have two tables with a Many to One Cardinality between table1 and table2 using [CustCode]
Table1
+-----+--------+----------+--------+
|Order|CustCode| CustName | Sales$ |
+-----+--------+----------+--------+
| 1 | 1A | Jack | 5,000 |
| 2 | 2A | Jill | 20,000 |
| 3 | 3A | Bill | 3,000 |
| 4 | 3B | Bill | 2,700 |
+-----+--------+----------+--------+
Table2
+--------+--------+------------+
|CustCode|CustName| Type| Type2|
+--------+--------+-----+------+
| 1A | Jack | OEM | 1 |
| 2A | Jill | DIST| 1 |
| 3A | Bill | DIST| 1 |
+--------+--------+-----+------+
Table1 is brought in using PowerQuery from a source which updates daily. Table 2 is a table in the existing workbook. Both are pulled into a "Data Model" in the same workbook using PowerPivot.
How can I dynamically take new records that appear in table1 (i.e., Order 4) with its new CustCode and place it into Table2 and place the same values for the Type and Type 2 fields because it's the same Customer (Bill) and his Type is a DIST and his Type2 is 1?

MYSQL Insert into table's column based on column name

For some reason I am having difficulty wording this question but I will try my best. I've been searching for 2 days on and off now and haven't found a good solution to the issue.
I have a Table called InventoryNode;
_________________________________________________
| InvID | ID | Slot | ItemID1 | ItemID2 | ItemID3 |
|-------|----|------|---------|---------|---------|
| 1 | 1 | Neck | 10 | 22 | 66 |
| 1 | 2 | Head | 26 | 23 | 56 |
| 1 | 3 | Leg | 19 | 21 | 76 |
And another table called Inventory which stores the Node ID in each column
_____________________________
| ID| Neck | Head | Leg | ... |
|---|------|------|-----|-----|
| 1 | 1 | 2 | 3 | 66 |
If there a way I can insert the node ID's into the Inventory table based off the InvID and populate all the columns with the correct name with the Node's ID?
Something like this?
INSERT INTO Inventory INNER JOIN InventoryNode ON
(Inventory.ID = InventoryNode.InvID) WHERE Inventory.column_name =
InventoryNode.Slot SET InventoryNode.InvID

Conditionally move MySQL data between rows in same table

Working in Redmine, I need to copy(not move) data from certain rows to other rows based on matching project id numbers with time entries.
I have included a diagram of the table "custom_values" and my understanding of the design below(CURRENT DATA):
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | |
+----+-----------------+---------------+-----------------+-------+
At the risk of oversimplifying,
"id" = The primary key for each entry in custom_values
"customized_type" = Specifies which db table the row is referring to.
"customized_id" = Specifies the primary key for the db table entry previously specified in "customized_type".
"custom_field_id" = Specifies which custom field the row is referring to. Redmine admins can arbitrarily add and remove custom fields.
"value" = The data contained within the custom field specified by
"custom_field_id"
In my situation, the values listed in "value" are representing unique customer id numbers. The customer id numbers did not always get entered with each time entry. I need to copy the customer numbers from the project rows to the matching time entry rows. Each time entry has a project_id field.
So far, here is my mangled SQL query:
SELECT
custom_field_id,
custom_values.value AS 'CUSTOMER_NUMBER',
custom_values.customized_id AS 'PROJECT_ID_NUMBER',
custom_values.customized_type,
time_entries.comments AS 'TIME_ENTRY_COMMENTS'
FROM
redmine_tweaking.custom_values
LEFT JOIN
redmine_tweaking.time_entries ON custom_values.customized_id = time_entries.project_id
WHERE
custom_values.customized_type='Project' AND custom_values.custom_field_id=1;
The query I have so far allows me to see that I have the time entries connected properly to their matching projects, but that is all I have been able to figure out. So in other words, this SQL statement does not exactly solve my problem.
Plus, even if it did work, I think the way I laid it out looks like 200 lbs of bird poop. There must be a better/more optimized way to do this.
Any help would be greatly appreciated. I am relatively new and I have been pouring hours into solving this problem.
UPDATE:
Ok, here is the time_entries table:
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| id | project_id | user_id | issue_id | hours | comments | activity_id | spent_on | tyear | tmonth | tweek | created_on | updated_on |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
| 1 | 1 | 1 | 1 | .25 | test | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 08:18:12 | 2015-11-04 10:18:12 |
| 2 | 2 | 1 | 1 | .25 | test2 | 9 | 2015-11-04 | 2015 | 11 | 45 | 2015-11-04 09:18:12 | 2015-11-04 12:18:12 |
+----+------------+---------+----------+-------+----------+-------------+------------+-------+--------+-------+---------------------+---------------------+
As opposed to the original table that I first posted, the expected output would show this:
+----+-----------------+---------------+-----------------+-------+
| id | customized_type | customized_id | custom_field_id | value |
+----+-----------------+---------------+-----------------+-------+
| 1 | Project | 1 | 1 | 01 |
| 2 | TimeEntry | 1 | 4 | 01 |
| 3 | Project | 2 | 1 | 02 |
| 4 | TimeEntry | 2 | 4 | 02 |
| 5 | Project | 3 | 1 | 03 |
| 6 | TimeEntry | 3 | 4 | 03 |
| 7 | Project | 4 | 1 | 04 |
| 8 | TimeEntry | 4 | 4 | 04 |
+----+-----------------+---------------+-----------------+-------+

Find the First occurrence value of data in Query

I have a table that has a lot of data, but only of four categories. I would like to return the ID of the First occurrence and last occurrence, of the type. The following is the simplified version of my data..
ID | FName | Password | Category | Outcome
-----------------------------------------------------------------
1 | Dan | something | NEW | 8
2 | Faye | another | NEW | 1
:
:
189 | Chris | Password | OLD | 2
190 | Matt | Milk | OLD | 7
:
:
1169 | Mark | Dog | LITE | 3
1170 | Nick | Land | LITE | 1
So I would like to have a query that will return the result as
CATEGORY | ID_START | ID_END
----------------------------------------
NEW | 1 | 188
OLD | 189 | 1168
LITE | 1169 | 9999
I am using Access 2010. Any help greatly appreciated.
I can sugest this:
select category, min(id) as idStart, max(id) as idEnd
from tbl
group by category
Hope this helps you