Solved

MySQL - how to create and select temp tables?

Posted on 2016-09-16
8
67 Views
Last Modified: 2016-09-16
Hi,
I am just wondering how I would go about creating and selecting a temp MySQL table?

Can it be the same name as an existing table.

I want to reduce size of existing "wp_ab_staff_locations" table to a specific location.

Thanks in advance.

CREATE TEMPORARY TABLE IF NOT EXISTS wp_ab_staff_locations AS (SELECT * FROM wp_ab_staff_locations WHERE location_id= '754' )
SELECT * FROM wp_ab_staff_locations;


CREATE TABLE IF NOT EXISTS `wp_ab_staff_locations` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `staff_id` int(10) unsigned NOT NULL,
  `location_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_ids_idx` (`staff_id`,`location_id`),
  KEY `location_id` (`location_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=96 ;

--
-- Dumping data for table `wp_ab_staff_locations`
--

INSERT INTO `wp_ab_staff_locations` (`id`, `staff_id`, `location_id`) VALUES
(9, 1, 200),
(10, 1, 202),
(1, 1, 757),
(4, 1, 770),
(5, 1, 771),
(6, 1, 773),
(7, 1, 774),
(8, 1, 776),
(37, 3, 754),
(38, 3, 755),
(39, 3, 756),
(40, 3, 757),
(41, 3, 758),
(42, 3, 759),
(43, 3, 760),
(87, 5, 754),
(88, 5, 755),
(89, 5, 756),
(94, 5, 761),
(95, 5, 762);

Open in new window

0
Comment
Question by:sabecs
  • 4
  • 2
  • 2
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802488
>>Can it be the same name as an existing table.
why you intend to do that?

>>I want to reduce size of existing "wp_ab_staff_locations" table to a specific location.
So why you move your data into a temporarily table? Why you don't store your existing data  into an archive table (another table) instead?
0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41802491
Here you go few sample examples..

--

CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)

CREATE TEMPORARY TABLE top10customers
SELECT p.customerNumber, 
       c.customerName, 
       FORMAT(SUM(p.amount),2) total
FROM payments p
INNER JOIN customers c ON c.customerNumber = p.customerNumber
GROUP BY p.customerNumber
ORDER BY total DESC
LIMIT 10

--

Open in new window

0
 

Author Comment

by:sabecs
ID: 41802527
Thanks for your comments, I haven't used temp tables before but from what I understand I can create for a new table for each visitor to my website specific to their location. So when they search for staff they will only see staff in their location?
0
 

Author Comment

by:sabecs
ID: 41802535
I have tried creating a temp table using phpMyAdmin form cPanel, seems to create table but I get an error when I try and view table?

CREATE TEMPORARY TABLE IF NOT EXISTS wp_ab_staff_locations AS (SELECT * FROM wp_ab_staff_locations WHERE location_id= '754' );
SELECT * FROM wp_ab_staff_locations;

 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM wp_ab_staff_locations' at line 2
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sabecs
ID: 41802537
sorry, I had a typo, it works well.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41802540
Great ! Could you please mark one solution as accepted solution and close the question.

Thank you !
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41802550
>>but from what I understand I can create for a new table for each visitor to my website specific to their location. So when they search for staff they will only see staff in their location

you NO need specifically to create temporary table for your visitor since your original table can serve the same purpose. And to store operational data (like visitor activities to your site) to temporary tables could be a risk if your MySQL Server facing error and being forced to restart the service. Your data could be gone.

just write to physical tables would be a better approach. use temporary tables for other purposes instead.
0
 

Author Closing Comment

by:sabecs
ID: 41802568
Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now