LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2006, 12:07 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
MySQL: Help with using join to simulate subquery


Hey y'all... I have this MySQL table...

Code:
CREATE TABLE `table1` (
  `uin` int(11) NOT NULL auto_increment,
  `name` varchar(50) not null,
  `email` varchar(100) not null,
  `body` blob,
  PRIMARY KEY  (`uin`)
);
which I need to create a new table from excluding older rows which have a duplicate 'body' field. I have done this in MySQL 5 using a subquery:

Code:
CREATE TABLE table1 AS (
SELECT * FROM table1 WHERE uin IN
(SELECT MAX(uin) FROM table1 GROUP BY body)
);
However, it's giving an error in older MySQL. Is there a tricky way of doing a join with the same table to achieve the same effect?
 
Old 09-30-2006, 07:17 AM   #2
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
Just put the result of your subquery in a temporary table and use it as a join to populate the other one.
Code:
CREATE TEMPORARY TABLE singleuin as SELECT MAX(uin) as maxuin FROM table1 GROUP BY body;
CREATE TABLE table2 AS SELECT table1.* FROM table1 JOIN singleuin ON table1.uin = singleuin.maxuin;
Once your mysql session finishes the singleuin temporary table will disapear living only table 1 and table 2

On the other hand, you can do this in only one query
Code:
CREATE TABLE table2 AS SELECT MAX(uin), name, email, body FROM table1 GROUP BY body;
Meanwhile... my prefered method would be to create the empty table and populate it with an 'insert into... select' statement.
Code:
CREATE TABLE `table2` (
  `uin` int(11) NOT NULL auto_increment,
  `name` varchar(50) not null,
  `email` varchar(100) not null,
  `body` blob,
  PRIMARY KEY  (`uin`)
);
INSERT INTO table2 (uin, name, email, body) SELECT MAX(uin), name, email, body FROM table1 GROUP BY body;

Last edited by zeitounator; 09-30-2006 at 07:23 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to join domain using Net Join command in FC3 client jeb083079 Linux - Networking 9 07-30-2007 02:41 AM
LXer: Learn how to join tables in MySQL LXer Syndicated Linux News 0 08-08-2006 03:21 PM
MySQL Join Syntax Issue PerfectReign Programming 5 05-11-2006 08:24 AM
Subquery mysql dont work cuencano General 1 08-03-2005 12:30 AM
MySQL: Subquery in HAVING clause ugenn Programming 2 08-11-2002 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration