Solved

MySQL - how to create and select temp tables?

Posted on 2016-09-16
8
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 52

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 29

Accepted Solution

by:
Pawan Kumar 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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
 

Author Comment

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

Expert Comment

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

Thank you !
0
 
LVL 52

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

728 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