MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't
ask)
I have database Spring with table Students
I have database Summer with table Students
I am tasked to produce a query of all students in both tables with no
duplicates. No clue whatsoever.
I'm thinking an INTERSECT followed by 2 UNION SELECT statements, but
how far off am I? The tables are absolutely identical in every way
including indexes; the data MIGHT be identical (same data might be in
Spring.Students and Summer.Students ), however, that won't always be the
case. The column Spring.Students .id will not share the same value as
Summer.Students .id as data can be entered within separate sequences
even if the data in both tables is absolutely identical.
Am I on the right track with INTERSECT/UNION or what do you recommend?
Thanx
Phil
Nov 23 '05
48 3876
> It turns out that trying to add a table comment to MyISAM tables version 7 with 5.0.15 destroys them. dumping/restoring the tables produce a mixture of version 9 or 10. (You can see the version number of a table with SHOW TABLE STATUS.)
This is interesting, because new to me. Do you know if that problem still
exists in the new table versions? I also seem to have a mixture of version 9
and 10. Is that a problem?
Markus
Sorry, the "distinct" keyword failed to produce truly unique records.
The Spring.Students table (and the Summer.Students table) has about 75
columns, 74 of them are identical, the "id" column is unique.
I did find a rather novel approach to this, however:
[SQL]
-- field `unique_key` will contain a 16-character random alphanumeric
string for repost protection
ALTER TABLE Spring.Students ADD UNIQUE (`unique_key`);
ALTER TABLE Summer.Students ADD UNIQUE (`unique_key`);
DROP TABLE /*! IF EXISTS */ AllStudents.spr ing_summer;
CREATE TABLE /*! IF NOT EXISTS */ AllStudents.spr ing_summer SELECT *
FROM Spring.Students LIMIT 1;
DELETE FROM AllStudents.spr ing_summer;
INSERT INTO AllStudents.spr ing_summer SELECT * FROM Spring.Students
UNION SELECT * FROM Summer.Students ;
[/SQL]
This produces the following syntax error:
Syntax error near 'UNION SELECT * FROM Summer.Students '
What gives? It was working over the weekend and now fails? WHY?
Phil
Markus Popp wrote: Use the DISTINCT keyword:
select distinct col1, col2 from spring_summer; (list all the columns that are non-unique, so leave out the ID column)
There are also ways to eliminate the duplicate records, but (with versions before 4.1) that would require that you create another (temporary) table. This looks something like this:
create table temp_table select min(id) as id, col1, col2 from spring_summer group by col1, col2;
This produces a temporary table with all unique records with their lowest ID (you could also use max(id) to get the highest ID).
Then you can join the temp_table with the spring_summer to find all records that have another ID than those saved in the temp_table. Look at this:
mysql> select * from tbl; +----+---+ | id | d | +----+---+ | 1 | a | | 2 | a | | 3 | b | | 4 | b | | 5 | b | | 6 | c | | 7 | c | +----+---+ 7 rows in set (0.01 sec)
mysql> create table temp_table select min(id) as id, d from tbl group by d; Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from temp_table; +------+---+ | id | d | +------+---+ | 1 | a | | 3 | b | | 6 | c | +------+---+ 3 rows in set (0.00 sec)
mysql> delete a from tbl a left join temp_table b on a.id = b.id where b.id is null; Query OK, 4 rows affected (0.00 sec)
I'm not sure if the last query works with MySQL 3.23. If it doesn't, it would take some other extra steps (that's were we see the benefits of MySQL 4.1 and 5.0, where this all could be done in a single statement).
Markus
Ok UPDATE:
We upgraded to MySQL 5.0 and I tried this, and this also failed to
function:
[SQL]
DROP TABLE /*! IF EXISTS */ AllStudents.spr ing_summer;
CREATE TABLE /*! IF NOT EXISTS */ AllStudents.spr ing_summer SELECT *
FROM OrigStudents.st udents LIMIT 1;
DELETE FROM AllStudents.spr ing_summer;
ALTER TABLE AllStudents.spr ing_summer ADD UNIQUE (unique_key);
INSERT INTO AllStudents.spr ing_summer SELECT * FROM Spring.Students
UNION SELECT * FROM Summer.Students ;
[/SQL]
This in MySQL 5.0 throws an error after only inserting one record:
Duplicate entry 'asdfasdf' for key 1
I can't figure this out, it's literally due AM Monday; I've been at
this for a week now (it was due earlier this week) and I can't combine
two tables into 1 with unique data!
Phil
>Ok UPDATE: We upgraded to MySQL 5.0 and I tried this, and this also failed to function:
[SQL] DROP TABLE /*! IF EXISTS */ AllStudents.spr ing_summer;
CREATE TABLE /*! IF NOT EXISTS */ AllStudents.spr ing_summer SELECT * FROM OrigStudents.st udents LIMIT 1;
DELETE FROM AllStudents.spr ing_summer;
ALTER TABLE AllStudents.spr ing_summer ADD UNIQUE (unique_key);
INSERT INTO AllStudents.spr ing_summer SELECT * FROM Spring.Students UNION SELECT * FROM Summer.Students ;
Use INSERT IGNORE here and let the errors happen, but it should
keep going..
Gordon L. Burditt ph************* *@gmail.com wrote: I can't figure this out, it's literally due AM Monday; I've been at this for a week now (it was due earlier this week) and I can't combine two tables into 1 with unique data!
You need to get help from the teacher. By the way, internet etiquette
generally frowns upon students trying to get help with their homework on
newsgroups.
Bill
Ok UPDATE:
We upgraded to MySQL 5.0 and I tried this, and this also failed to
function:
[SQL]
DROP TABLE /*! IF EXISTS */ AllStudents.spr ing_summer;
CREATE TABLE /*! IF NOT EXISTS */ AllStudents.spr ing_summer SELECT *
FROM OrigStudents.st udents LIMIT 1;
DELETE FROM AllStudents.spr ing_summer;
ALTER TABLE AllStudents.spr ing_summer ADD UNIQUE (unique_key);
INSERT INTO AllStudents.spr ing_summer SELECT * FROM Spring.Students
UNION SELECT * FROM Summer.Students ;
[/SQL]
This in MySQL 5.0 throws an error after only inserting one record:
Duplicate entry 'asdfasdf' for key 1
I can't figure this out, it's literally due AM Monday; I've been at
this for a week now (it was due earlier this week) and I can't combine
two tables into 1 with unique data!
Phil
There is no teacher, class, whatever. This is work and it's due Monday
AM!
Phil
Bill Karwin wrote: ph************* *@gmail.com wrote: I can't figure this out, it's literally due AM Monday; I've been at this for a week now (it was due earlier this week) and I can't combine two tables into 1 with unique data!
You need to get help from the teacher. By the way, internet etiquette generally frowns upon students trying to get help with their homework on newsgroups.
Bill ph************* *@gmail.com wrote: INSERT INTO AllStudents.spr ing_summer SELECT * FROM Spring.Students UNION SELECT * FROM Summer.Students ;
This in MySQL 5.0 throws an error after only inserting one record:
Duplicate entry 'asdfasdf' for key 1
Well, this means that there are some entries in Spring.Students that
also exist in Summer.Students . Therefore you get a conflict as soon as
the insert reaches the first duplicate (maybe the first record returned
by the second half of the UNION).
At the beginning of this thread I suggested a query construction that
should work. Here it is again, with the table names matching your query
above.
INSERT INTO AllStudents.spr ing_summer
(SELECT S1.*
FROM Spring.Students AS S1 INNER JOIN Summer.Students AS U1 ON
S1.unique_key = U1.unique_key)
UNION
(SELECT S2.*
FROM Spring.Students AS S2 LEFT OUTER JOIN Summer.Students AS U2 ON
S2.unique_key = U2.unique_key
WHERE U2.unique_key IS NULL)
UNION
(SELECT U3.*
FROM Spring.Students AS S3 RIGHT OUTER JOIN Summer.Students AS U3 ON
S3.unique_key = U3.unique_key
WHERE S3.unique_key IS NULL);
Regards,
Bill K.
You know the old adage, "Ignore the problem and it'll go away"? That's
exactly what I did, and everything worked:
DROP TABLE AllStudents.stu dents;
CREATE TABLE AllStudents.stu dents SELECT * FROM Spring.Students LIMIT
1;
ALTER TABLE AllStudents.stu dents ADD UNIQUE (email);
ALTER TABLE AllStudents.stu dents ADD UNIQUE (unique_key);
INSERT IGNORE INTO AllStudents.stu dents SELECT * FROM Spring.Students
UNION DISTINCT SELECT * FROM Summer.Students ;
That did the trick! In short, enforce uniqueness on 2 of the columns
that should be unique, which ensures "duplicates " are never added while
warnings are thrown out, so that all unique records are combined into
one table!
The only stipulation was that we had to do an enforced upgrade to MySQL
5.0 to ensure this would work.
Phil
Bill Karwin wrote: ph************* *@gmail.com wrote: INSERT INTO AllStudents.spr ing_summer SELECT * FROM Spring.Students UNION SELECT * FROM Summer.Students ;
This in MySQL 5.0 throws an error after only inserting one record:
Duplicate entry 'asdfasdf' for key 1
Well, this means that there are some entries in Spring.Students that also exist in Summer.Students . Therefore you get a conflict as soon as the insert reaches the first duplicate (maybe the first record returned by the second half of the UNION).
At the beginning of this thread I suggested a query construction that should work. Here it is again, with the table names matching your query above.
INSERT INTO AllStudents.spr ing_summer (SELECT S1.* FROM Spring.Students AS S1 INNER JOIN Summer.Students AS U1 ON S1.unique_key = U1.unique_key) UNION (SELECT S2.* FROM Spring.Students AS S2 LEFT OUTER JOIN Summer.Students AS U2 ON S2.unique_key = U2.unique_key WHERE U2.unique_key IS NULL) UNION (SELECT U3.* FROM Spring.Students AS S3 RIGHT OUTER JOIN Summer.Students AS U3 ON S3.unique_key = U3.unique_key WHERE S3.unique_key IS NULL);
Regards, Bill K. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Scotter |
last post by:
Okay I think my title line was worded misleadingly. So here goes again.
I've got quite 20 identical MDB files running on an IIS5 server.
From time to time I need to go into various tables and add a field or two.
It would be great if there were an application out there that could either:
(a) sync all MDB designs (and/or data) designated to match one I've added
some fields/tables to
OR
(b) go into all designated MDBs and create new...
|
by: Andreas Lauffer |
last post by:
I changed from Access97 to AccessXP and I have immense performance
problems.
Details:
- Access XP MDB with Jet 4.0 ( no ADP-Project )
- Linked Tables to SQL-Server 2000 over ODBC
I used the SQL Profile to watch the T-SQL-Command which Access ( who
creates the commands?) creates and noticed:
|
by: James Fortune |
last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't
understand why people who know how to write code to completely replace
a front end do not write something that will automate the code that
implements managing unbound controls on forms given the superior
performance of unbound controls in a client/server environment. I can
easily understand a newbie using bound controls or someone with a
tight deadline. I guess I need...
|
by: Reidar Jorgensen |
last post by:
I have several Access databases, identical in structure, but different data. Is there an
easy way to combine them all into one database? There are six tables, I just want the
data from all databases merged into one database.
Thanks in advance.
|
by: Cheryl Langdon |
last post by:
Hello everyone,
This is my first attempt at getting help in this manner. Please
forgive me if this is an inappropriate request.
I suddenly find myself in urgent need of instruction on how to
communicate with a MySQL database table on a web server, from inside
of my company's Access-VBA application. I know VBA pretty well but
have never before needed to do this HTTP/XML/MySQL type functions.
| |
by: Veeru71 |
last post by:
We have got 2 DB2-UDB databases (DB1 & DB2) running on separate
instances( Inst1 & Inst2).
DB1 has got Schema1 and DB2 has got Schema2.
We would like to setup some kind of replication to replicate both
Schema1 & Schema2 onto a 3 rd database (DB3) running on a 3 rd
instance (Inst3). This is basically to separate out Data Reads
(Reports module of the app) from the primary databases for performace
reasons. Also, Some of the Reports join...
|
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= |
last post by:
I get the above error in some of the ASP.NET web applications on a
server, and I need some help figuring out how to deal with it.
This is a rather long post, and I hope I have enough details that
someone who bothers to read all of it have some pointers.
Note, I have posted the stack trace and the code exhibiting the problem
further down so if you want to start by reading that, search for +++
Also note that I am unable to reproduce...
|
by: Okonita via DBMonster.com |
last post by:
Hi all,
I am comming along with all this Linus/DB2/scripting business...I am no
longer scared of it!! (LOL). But,
I need to create a .ksh script that does a REORGCHK and output only tables
recommended for reorg.
My goal is to reorgchk and run reorgs based on entries in this reorg file as
shown in the example below.
I have tried my hand at the following failing script and hope that gurus here
can throw me a lifeline of examples on how to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |