简体   繁体   中英

MySQL remove duplicated row data on SELECT

I have a table that i want to filter out duplicated data when using SELECT operation, the table itself contain data like this:

|=======|
| SPEED |
|=======|
|  100  |
|  100  |
|   90  |
|   80  |
|   80  |
|  100  |
|   80  |
|=======|

What i want to is something like this, notice that the order still intact:

|=======|
| SPEED |
|=======|
|  100  |
|   90  |
|   80  |
|  100  |
|   80  |
|=======|

DISTINCT or GROUP BY didn't work since it's discard all duplicated data

Why i need this kind of data, is because i want to draw chart using this data, by reducing the node (removing duplicated data in some sequences) the chart would less crowded and faster to render

Edited, since the question was clarified.

In order to archieve something like this, your table needs to have another column, lets call it Id . My test table looks like this:

CREATE TABLE `yourtable` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Speed` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=MyISAM

In order to check if the following row is maybe the same value for the field speed then you might just use a LEFT JOIN and leave out all the data where both values equal.

Try this:

SELECT A.Id, A.Speed, B.* 
FROM yourtable AS A
LEFT JOIN yourtable AS B ON A.Id+1 = B.Id
WHERE A.Speed != B.Speed OR ISNULL(B.Speed);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM