I have a project in ruby on rails 3.0.I have a database schema in pg. I have the instance of this project on 2 servers with respective databases.Now I have to shift everything to one server.So how do I export data from one database to the other?It can not be a literal export-import of databases bcos it has many tables with id and many to many relationships.So basically I need to append it so that there s no conflict for example
Databse 1 table 1 user
id Name
1 Josh
2 Rajn
4 Kush
Database table 1 user
id Name
1 Ram
2 Kevin
7 Don
So the new should be
Databse table 1 user
id Name
1 Ram
2 Kevin
7 Don
8 Josh
9 Rajn
10 Kush
and the join tables should have the new ids too
Not down the maximum value of the id field from all the target tables and add them as the offset values to the source id fields.
Ex:
migration.sql
SELECT #max_user_id := MAX(id) FROM users;
SELECT #max_comment_id := MAX(id) FROM comments;
# Then perform the following mysql commands in the target database:
INSERT INTO target.users(id, name) SELECT id + #max_user_id, name FROM source.users
INSERT INTO target.comments(id, comment, user_id) SELECT id + #max_comment_id, comment, user_id + #max_user_id FROM source.comments
Note that you cannot do the migration in
Related
I'm trying to write a SQL query to find the lowest available/unused ID from a column named internal that exists in two separate tables:
machines
machines_ignore
Data is processed from an external source, and we want to fetch data from all machines that are not in the machines_ignore table. The ignore table is just a manual table set up by us when we identify machines we don't want to analyze.
I've found scripts that work on a single table (like only the machines table), but as soon as I try to get it working when combining two tables.
Example
Table 1 (machines)
id
internal
1
1
2
2
3
3
4
5
5
6
Table 2 (machines_ignore)
internal
4
7
8
9
12
Expected result
Based on the example above, this query should output 10, 11, 13 etc.
Any ideas?
One solution is to combine the values from both tables then check if each value has next value in both tables using EXISTS:
SELECT x.internal + 1
FROM (
SELECT internal FROM machines
UNION
SELECT internal FROM machines_ignore
) AS x
WHERE NOT EXISTS (
SELECT 1 FROM machines WHERE internal = x.internal + 1
) AND NOT EXISTS (
SELECT 1 FROM machines_ignore WHERE internal = x.internal + 1
)
LIMIT 1
I am writing a query sql for a data migration job to fetch data and send from mysql-server a to mysql-server b.
Server a has different databases, representing different game channel, and each database has a table tablex, they have same table name and same schema:
uid level
123 3
211 5
While in server b there is only one table tablex to receive tablex of all databases and it has one more column - channel
channel uid level
1 123 3
1 211 5
2 355 2
I can parse channel number from db name via python but I need to put this constant in the sql and since there are many tables, I cannot fix the columns. So just want to make sure is there any way to do this like:
select 1,* from xxx.yyy
You could try adding alias and table name
select 1 as my_col, yyy.* from xxx.yyy
or using string
select cast('1' as unsigned) , yyy.* from xxx.yyy
Yes, you can wrap the query into another SELECT and add the columns, e.g.:
SELECT A.*, '1'
FROM (
SELECT *
FROM your_table
) A;
I have a MySQL database for tracking projects. Each project has one or more categories, and each category has one or more tasks.
--------
PROJECTS
--------
ID Name
1 My First Project
----------
CATEGORIES
----------
ID ProjectID Name
1 1 My First Category for Project 1
2 1 My Second Category for Project 1
----------
Tasks
----------
ID ProjectID CategoryID Name
1 1 1 My First Tasks for Category 1, Project 1
2 1 1 My Second Task for Category 1, Project 1
3 1 2 My First Task for Category 2, Project 1
(I know that ProjectID is not required for the Tasks table.)
When a user initiates a new project, I'd like to include a dozen or so default category rows (and their associated default tasks).
Needless to say, I can't execute purely canned INSERT instructions, because I need to know the ID numbers for each newly inserted project before I insert its categories, just as I would need to know each new category's ID before I could insert its tasks.
Of course I can do this with a series of queries, executed with PHP, but I'm wondering if this is something that can be handled with either a stored procedure or some sort of fancier-than-I-know-how-to-write mega query.
I want to make a table to save the user log ( for example : login record)
Can the table check the id when using the auto increment ?
Example :
userid times login_record
1 1 xxxxx
2 1 xxxyy
1 2 xxyyy
1 3 xyyyy
2 2 xxxxx
not the way you mean, no. Mysql will not automatically look up the last row in the logins table with a matching user_id and increment the times field.
the mysql auto-increment is for the internally generated row id, not for column values.
If you insert one row for every login, you can get the user logins count by querying the table, select count(*) from logins where user_id = 1. If you add an index on user_id, this query will run pretty quick.
I have a table called Notes and column name as id and name
eg datas
Id name
1 DEV
2 Prod
3 Prod
4 Prod
5 SQL
From this above table I need to retrive the value like this
Id name
1 DEV
2 Prod
5 SQL
SELECT MIN(id), Name
FROM Notes
GROUP BY Name