Search code examples
mysqltemp-tables

Mysql Temporary Table Syntax


I have a question about the syntax for creating and accessing temporary tables.
Here is a related question.

My table

CREATE TABLE IF NOT EXISTS `table1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `table1`
--

INSERT INTO `table1` (`id`, `name`, `address`) VALUES
(1, 'andrew', '5 road'),
(2, 'bob', '6 street');

I am running this query.

CREATE TEMPORARY TABLE temptable SELECT id, name, address
FROM table1
SELECT id, name, address
FROM temptable

And tried this one

CREATE TEMPORARY TABLE temptable SELECT id, name, address
FROM table1
DESCRIBE temptable

Creating the temp table works, but then when I try to get info out of the temp table I get a message saying I need to check my sql syntax. thanks andrew


Solution

  • I left out the ';' after each statement. My query should have looked like this

    CREATE TEMPORARY TABLE temptable SELECT id, name, address
    FROM table1;
    SELECT id, name, address
    FROM temptable;
    

    Details are important in programming and so is stackoverflow