2012-08-23 44 views
0

我是新的mySQL用户使用UNION命令添加2个查询表。如果他们具有相同的ID,那么除了结果之外的联合工作会重复。如果我通过phpMyAdmin删除或更改ID,您会看到该条目。例如:”UNION表查询错误数据

My day | today | 12:00 
My day | today | 12:00 

和ID的变化:

OK day | other | 12:01 
My day | today | 12:00 

而且PHP代码

$query = "SELECT id,title,day,time 
      FROM $db_1 
      UNION 
      SELECT id,title,day,time 
      FROM $db_2 
      WHERE month='$month' AND year='$year' ORDER BY time" ;enter code here 
$query_result = mysql_query ($query); 
while ($info = mysql_fetch_array($query_result)) 

{ 
$day = $info['day']; 
$event_id = $info['id']; 
$events[$day][] = $info['id']; 
$event_info[$event_id]['0'] = substr($info['title'], 0, 8);; 
$event_info[$event_id]['1'] = $info['time']; 
} 

回答

0

你可以使用GROUP BY

SELECT * from 
(SELECT id,title,day,time FROM $db_1 
UNION 
SELECT id,title,day,time FROM $db_2 
WHERE month='$month' AND year='$year' ORDER BY time) m 
GROUP BY id 
+1

MySQL的不同是在删除重复有效得多。通过一个荒谬简单的查询的解释计划发现这一点时间过长。编辑我的答案,显示一个我刚刚生成的 – Fluffeh

+0

@Fluffeh好的。将通过你的答案。谢谢。 – sel

+0

@Fluffeh感谢您运行和比较查询。确实是一个非常好的解释答案。 – sel

2

这应该做的骗你:

select distinct * 
from 
(
    SELECT 
     id, 
     title, 
     day, 
     time 
    FROM 
     $db_1 
    UNION ALL 
    SELECT 
     id, 
     title, 
     day, 
     time 
    FROM 
     $db_2 
    WHERE 
     month='$month' 
     AND year='$year' 
) 
ORDER BY time 

虽然group by可以使用,但效率较低。我在下面的查询中遇到了麻烦,最终运行了一些统计数据,因为第一个查询在大约10秒内返回,第二个查询在半秒钟之内。

不同的是,使用一组通过将引入一个文件排序的子查询,这在我的情况下引起的一切问题:

mysql> EXPLAIN select 
    -> a.mCode as cCode, 
    -> a.mName as cName 
    -> from 
    -> _user_UserStructure a 
    -> where 
    -> a.pCode in 
    -> (
    ->   select 
    ->     b.pCode 
    ->   from 
    ->     _user_userStructure b 
    ->   where 
    ->     b.mCode='RBM1' 
    ->     and b.pCode!=b.cCode 
    ->   group by 
    ->     b.pCode 
    -> ) 
    -> and a.cCode != '' 
    -> and a.pCode != a.mCode 
    -> and a.mCode!='RBM1' 
    -> group by 
    -> a.mCode, 
    -> a.mName 
    -> order by 
    -> a.mName asc; 
+----+--------------------+-------+-------+---------------+-------+---------+------+------+----------------------------------------------+ 

| id | select_type  | table | type | possible_keys | key | key_len | ref | rows | Extra          | 

+----+--------------------+-------+-------+---------------+-------+---------+------+------+----------------------------------------------+ 

| 1 | PRIMARY   | a  | ALL | cCode   | NULL | NULL | NULL | 1769 | Using where; Using temporary; Using filesort | 

| 2 | DEPENDENT SUBQUERY | b  | index | NULL   | pCode | 13  | NULL | 2 | Using where; Using filesort     | 

+----+--------------------+-------+-------+---------------+-------+---------+------+------+----------------------------------------------+ 

2 rows in set (0.00 sec) 


mysql> EXPLAIN select distinct 
    -> a.mCode as cCode, 
    -> a.mName as cName 
    -> from 
    -> _user_UserStructure a 
    -> where 
    -> a.pCode in 
    -> (
    ->   select distinct 
    ->     b.pCode 
    ->   from 
    ->     _user_userStructure b 
    ->   where 
    ->     b.mCode='RBM1' 
    ->     and b.pCode!=b.cCode 
    -> ) 
    -> and a.cCode != '' 
    -> and a.pCode != a.mCode 
    -> and a.mCode!='RBM1' 
    -> order by 
    -> a.mName asc; 
+----+--------------------+-------+----------------+---------------+-------+---------+------+------+----------------------------------------------+ 

| id | select_type  | table | type   | possible_keys | key | key_len | ref | rows | Extra          | 

+----+--------------------+-------+----------------+---------------+-------+---------+------+------+----------------------------------------------+ 

| 1 | PRIMARY   | a  | ALL   | cCode   | NULL | NULL | NULL | 1769 | Using where; Using temporary; Using filesort | 

| 2 | DEPENDENT SUBQUERY | b  | index_subquery | pCode   | pCode | 13  | func | 2 | Using where         | 

+----+--------------------+-------+----------------+---------------+-------+---------+------+------+----------------------------------------------+ 

2 rows in set (0.00 sec) 
+0

DISTINCT需要所有列s是一样的。如果两个表中的某些其他列不同,您仍然可能会得到重复的ID。 – Barmar

+0

如果您在外部查询中执行DISTINCT,则应该在内部查询中执行UNION ALL。没有理由让优化器甚至有两次删除重复的机会。 –

+0

@GordonLinoff优秀的一点。更新了答案。 – Fluffeh