Solved

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

Posted on 2016-08-22
4
50 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 52

Accepted Solution

by:
Ryan Chong earned 500 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 52

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

729 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