UNION表查询错误的数据

时间:2012-08-23 01:35:50

标签: php mysql union

我是新的mySQL用户使用UNION命令添加2个查询表。工会工作除了结果重复自己,如果他们有相同的ID。如果我通过phpMyAdmin删除或更改ID,您会看到该条目。例如:'

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'];
}

2 个答案:

答案 0 :(得分: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秒内返回,第二次查询是在不到半秒内。

不同之处在于使用group by会在子查询中引入一个filesort,在我的例子中会导致所有问题:

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) 

答案 1 :(得分: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