MySQL create table with select, alias column names - mysql

Is there any way to use a create table with select statement where column names in conflict (or all) are aliased?
CREATE TABLE newTable
SELECT a.*, b.*
FROM tblA a
JOIN tblB b
ON a.id = b.cid
The issue is that tblA and tblB have a few columns with the same name, so I get a "duplicate column name" error on the create. I'm trying to avoid listing all the fields in the table, so I either need to selectively exclude some columns or apply and "auto alias" to the column names.

You can use the information_schema table to selectively exclude columns in a select statement. See the top answer here.

Related

How to change table alias in MySQL query

I want to show employee's first name from employee table, but when I execute the query it always returns this error
#1066 - Not unique table/alias
This is my query:
SELECT `employee`.`Fname`
FROM `employee`
LEFT JOIN `company07`.`employee`
ON `employee`.`Ssn` = `employee`.`Super_ssn`
ORDER BY `employee`.`Fname` ASC
Q: Why is MySQL returning error 1066?
Q: How do I change the table alias to avoid error 1066?
There are two table references with the same name "employee". And this isn't valid.
The workaround is to assign an alias to one of the table references. That will make the names unique.
Frequently, we assign a short alias to every table reference. And we use that alias to qualify the column references.
In the following example, we assign the alias e to one of the tables, and the alias c to the other.
SELECT e.`fname`
FROM `employee` e
LEFT
JOIN `company07`.`employee` c
ON c.`ssn` = e.`super_ssn`
ORDER
BY e.`fname` ASC
I just guessed which table "ssn" is from and which table "super_ssn" is from. In the original query, it's ambiguous. Both are qualified with the table name employee. But we don't know which of the tables is being referred to. MySQL has the same problem... which table does "employee" refer to. And this is why MySQL requires that every table name (or alias) needs to be unique.

How do I migrate and sync up a table efficiently?

I have a table A, it has millions of records and it's growing. A new column needs be added to table A with indexing, but it could be headache to migrate such a large table. So table B is created at some point from table A, question is how to sync up these 2 tables efficiently?
There're multiple scenarios new records will be added to table A.
To 'Sync' up two tables without actually merging them you can create a UNION VIEW. A VIEW can be used just like a table for calculation, manipulation, data storage etc. This is assuming that both tables have the same amount of rows, if not you'll need to create primary and foreign keys.
CREATE OR REPLACE VIEW viewname AS
SELECT * FROM TABLE_A
UNION ALL
SELECT * FROM TABLE_B
Now if both tables don't share the same amount of rows you'll need at least 1 field in common between the two tables called primary and foreign keys, to join the tables using the primary and foreign keys you'll need to use a JOIN like this:
CREATE OR REPLACE VIEW viewname AS
SELECT TableA.FieldName, TableB.FieldName, TableA.FieldName
FROM TableA
LEFT JOIN TableB
ON TableA.primarykeyField = TableB.foreignkeyField
UNION ALL
SELECT TableA.FieldName, TableB.FieldName, TableA.FieldName
FROM TableA
RIGHT JOIN TableB
ON TableA.primarykeyField = TableB.foreignkeyField
It depends on what type of join you want but I think FULL JOIN will give you the best results, FULL JOIN's aren't supported in MySQL but using LEFT JOIN RIGHT JOIN and UNION ALL mimics the same results.
Or if you simply want to copy all the records from table A to table B you could use this.
INSERT INTO TableB
SELECT * FROM TableA;

Join table on earlier joined table

Say I have got three tables: A, B and C.
A has primary key a
B has primary key a and also a non-primary key c.
C has primary key c.
I want to begin selecting from table A.
So I got a query like this:
Select * from A join B on A.a=B.a join C on B.c=C.c
It returns
Unknown column 'B.c' in 'on clause''
Is this impossible in mysql, joining a table on a joined table? Or am I just doing something wrong? BTW table and column names a made up.
It is possible, and your syntax should be correct.
please refer to this link for syntax:
http://dev.mysql.com/doc/refman/5.0/en/join.html
example to interesting things you can do in MySQL (from the documentation):
SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
Also note the error claims that column C does not exist - so you should check the structure of B..

Deleting duplicates in mysql (2 tables)

I have two tables (id_test, test) , each of them has an ID column, which is unique, and two entries with the same id in the two tables are the same. Now, i have another column in one of the tables (id_test) that also should be unique, so I want to eliminate duplicates according to this other column, let's call it YD.
To identify the duplicates I used
SELECT ID, YD AS x, COUNT(*) AS y
FROM id_test
GROUP BY x
HAVING y>1;
now, I want to delete these entries in both tables. How can I do it?
This query shows the first ID for every YD in id_test table:
SELECT ID, YD
FROM id_test
GROUP BY YD
and these are the rows you have to keep. The following query returns the IDs you have to delete:
SELECT id_test.ID
FROM id_test LEFT JOIN (select ID, YD from id_test group by YD) id_test_keep
on id_test.ID=id_test_keep.ID and id_test.YD = id_test_keep.YD
WHERE id_test_keep.ID IS NULL
Now I think i need more details about your tables, but what I think you need is this:
DELETE FROM test
WHERE
test.ID IN (
SELECT id_test.ID
FROM id_test LEFT JOIN (select ID, YD from id_test group by YD) id_test_keep
on id_test.ID=id_test_keep.ID and id_test.YD = id_test_keep.YD
WHERE id_test_keep.ID IS NULL)
As documented under ALTER TABLE Syntax (emphasis added):
IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key. The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.
Therefore:
ALTER IGNORE TABLE id_test ADD UNIQUE (YD)
I think you don't user select in because if data large it impossible.
You should clone a table the same structure. Insert data not duplicate in it.
INSERT INTO test_new (ID, YD) SELECT t.ID, t.YD FROM test t LEFT JOIN test_id ti ON t.ID = ti.id WHERE ti.id IS NULL;
After drop table test, rename test_new -> test.

Syncing primary key between two tables stored in two different database

I have two MySQL database that contain two table, let's call them TABLE_A and TABLE_B. Both these tables have as fields id and title. Both databases are on the same server and the same user can access both.
Now, TABLE_A is a subset of TABLE_B for what concern the title field. It means that every title in TABLE_A is present in TABLE_B, too. While id fields of the two table are in no way related.
What I need is to sync id fields in TABLE_A with id fields in TABLE_B, according to the title, i.e. same title, same id. If it's not clear, I have to save TABLE_B id and override TABLE_A ones. And I DON'T have to add missing title from TABLE_B to TABLE_A.
Someone suggested to use a temporal table where to copy all TABLE_B fields in common with TABLE_A and then rename it as TABLE_A. I cannot follow this way, as TABLE_A actually has also other fields that I need to maintain. So, I cannot entirely drop the old TABLE_A.
Moreover, id is the primary key for both tables. It means that I cannot simply copy from TABLE_B to TABLE_A as the query will fail as soon as I try to change one id to another one that is already present in TABLE_A but linked to a different title.
I know how to write a Perl or PHP script to do it, but I would like to know if a pure MySQL solution exists.
You can do this
CREATE TABLE TableA_TMP AS
SELECT * FROM TableA;
ALTER TABLE TableA_TMP ADD id_new int;
UPDATE TableA_TMP A INNER JOIN TableB B ON lower(A.title) = lower(B.title)
SET id_new = B.id;
RENAME TABLE TableA TO TableA_backup;
CREATE TableA AS
select id_new as id, title,.... from TableA_TMP;