我有两个包含地址数据的表。以下是部分代表:
表1(主要数据):
id address city state
1 1234 main st seattle wa
2 2345 oak st portland or
3 3456 maple st boise id
和表2(二手数据):
id address city state to_insert
1 4567 first st sacramento ca 1
2 5678 second st phoenix az 0
3 2345 oak st portland or 1
我需要首先从表2中获取一个标记为to_insert(bit)的值列表,这些值已经存在于表1中,因此我可以手动编辑数据并插入它们(因此它们不会重复) 。如何从表1(主要数据)中已存在的表2(辅助数据)中获取重复列表?
答案 0 :(得分:1)
您可以使用EXISTS
获取table2
上的重复行:
SELECT * FROM table2 t2 WHERE EXISTS (
SELECT * FROM table1
WHERE t2.address = table1.address
AND t2.city = table1.city
AND t2.state = table1.state
);
您可以在此处找到一个有效的示例: http://sqlfiddle.com/#!9/110b9a/1/0
从INSERT
到table2
的{{1}}数据,您可以使用以下内容:
table1
答案 1 :(得分:1)
您可以使用不在子选择中的插入选择
public_zone
答案 2 :(得分:1)
如果您想要那些不重复的值
insert into table1 select * from table2 where
row(id,address,city,state) in (select * from table1);
如果您想要那些重复的值
{{1}}
答案 3 :(得分:0)
使用GROUP BY子句执行SELECT。假设name是要在以下位置找到重复项的列:
<?php
if (isset($_POST["submit"])) {
$name = (string) $_POST['name'];
$email = (string) $_POST['email'];
$message = (string) $_POST['message'];
$number = (string) $_POST['number'];
$tel = (string) $_POST['tel'];
$model = implode(',', $_POST['model_list']);
$captcha = (string) $_POST['captcha'];
$from = 'abc@domain.tld';
$to = 'cba@domain.tld';
$subject = 'Form service';
$body = "$name \n $email \n $tel \n $number \n $model \n $message \n";
try {
if (!$name) {
throw new Exception('Error 1');
}
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Error 2');
}
if($captcha > 10) {
} elseif ($captcha < 1) {
throw new Exception('Error 3');
}
if(!$number) {
throw new Exception('Error 4');
}
if(!$number) {
throw new Exception('Error 5');
}
if (!$message) {
throw new Exception('Error 6');
}
if(!empty($_POST['model_list'])) {
foreach($_POST['model_list'] as $model) {
}
} else {
throw new Exception('Error 7');
}
if (mail ($to, $subject, $body, $from, $captcha, $model)) {
$result = "Your message has been sent!";
} else {
throw new Exception('Your message has not been sent! Try again.');
}
} catch(Exception $e){
$result = "<center><div style='color:white;font-size:25px;font-weight:700;'>" . $e->getMessage() . "</div></center>";
}
echo $result;
}
这将返回第一列中名称值的结果,以及该值在第二列中出现的次数。