Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL - need to modify one table with contents from other table?

Posted on 2016-08-22
4
Medium Priority
?
54 Views
Last Modified: 2016-08-22
Hi,
Can anyone provide an SQL query to get all distinct `location_id` from staff_locations table and add to locations_2 table as `title`.
Perhaps emptying the `locations_2` table might be the best option first?

Thanks in advance.

CREATE TABLE IF NOT EXISTS `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=21 ;



INSERT INTO `staff_locations` (`id`, `staff_id`, `location_id`) VALUES
(9, 1, 200),
(10, 1, 202),
(1, 1, 757),
(2, 1, 758),
(3, 1, 759),
(4, 1, 770),
(5, 1, 771),
(6, 1, 773),
(7, 1, 774),
(8, 1, 776),
(9, 1, 1641),
(10, 1, 1642),
(11, 2, 1636),
(12, 2, 1637),
(13, 2, 1638),
(14, 2, 1639),
(15, 2, 1641),
(16, 2, 1642),
(17, 2, 1645),
(18, 2, 1646),
(19, 2, 1647),
(20, 2, 1648);



CREATE TABLE IF NOT EXISTS `locations_2` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT '',
  `position` int(11) NOT NULL DEFAULT '9999',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=203 ;



INSERT INTO `locations_2` (`id`, `title`, `position`) VALUES
(1, '200', 9999),
(2, '202', 9999);

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
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 total points
ID: 41766261
try this:
insert into locations_2
(title)
select location_id
from staff_locations a left join locations_2 b
on a.location_id = b.title
where b.title is null;

Open in new window

0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 41766263
>>Perhaps emptying the `locations_2` table might be the best option first?
it's depends... for the comment I posted above it will only inserting the new entries into table: locations_2
0
 

Author Comment

by:sabecs
ID: 41766359
Thanks Ryan, your query works well expect I discovered I need the title from another table called locations, I will accept your solution and re post another question.
0
 

Author Closing Comment

by:sabecs
ID: 41766361
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

610 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