Forum Moderators: open

Message Too Old, No Replies

remove duplicate result after UNION

         

Kai4ever

2:54 pm on Dec 24, 2006 (gmt 0)

10+ Year Member



can i remove duplicate result after i do UNION (NOT UNION ALL)?

example is like this: id(*) is auto increment
table 'attendance'
id(*)..... metric_no.........enter_time........... class
1............B0001 ......2006-12-24 08:01:00........BK1
2............B0002 ......2006-12-24 08:02:00........BK1
3............B0003 ......2006-12-24 08:03:00........BK1

next table i query:
table 'subject_table'
id(*)..... metric_no.........subject_code.......
1............B0001.............BENT3213.........
2............B0002.............BENT3213.........
3............B0003.............BENT3213.........
4............B0004.............BENT3213.........

the result that i want is after UNION
will show only:
id(*)..... metric_no.........subject_code.......
1............B0004.............BENT3213.........

basically what i want to accomplish here is to compare the 'attendance' table from date_range1 to date_range2 against table 'subject_table'.

So, if there is duplicate, i want to remove it.
If there is another way to archive this please tell me.
and all query that i have done:
//////////////////////////////////////////////////////////
SELECT
attendance.metric_no
FROM
attendance
WHERE
(attendance.enter_time >= '2006-12-24 08:00:00') AND
(attendance.enter_time <= '2006-12-24 09:00:00')

UNION

SELECT
`subject_table`.metric_no
FROM
`subject_table`
WHERE
(`subject_table`.subject_code = 'BENT3213')
//////////////////////////////////////////////////////////
result after this query:
id(*)..... metric_no........
1............B0001..........
2............B0002..........
3............B0003..........
4............B0004..........

i want to remove id1,id2,id3.

can this be done?
please help me.... :(
Thanks for reading my post.

syber

3:40 pm on Dec 25, 2006 (gmt 0)

10+ Year Member



I don't think you want a UNION here, a subquery should give you what you want.


SELECT metric_no
FROM subject_table
WHERE
subject_code = 'BENT3213' AND
metric_no NOT IN (SELECT metric_no FROM attendance)