0

Hi all i have smal problem with sql i just need the result without duplicate SQL Table:

CREATE TABLE `charts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `timespan` varchar(200) DEFAULT NULL,
  `visits` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=latin1;

SQl Insert:

INSERT INTO charts (timespan, visits)
VALUES "2016-02-14 10:00 PM", '1234')";

Result:

$result = mysql_query("SELECT * FROM charts");

while($row = mysql_fetch_array($result)) {
  echo $row['timespan'] . "\t" . $row['visits']. "\n";
}
Tim
  • 7
  • 5
  • 3
    Possible duplicate of [How to select records without duplicate on just one field in SQL?](http://stackoverflow.com/questions/12239169/how-to-select-records-without-duplicate-on-just-one-field-in-sql) – Mr. Engineer Feb 14 '16 at 18:13
  • 1
    Seriously, did you [google](https://www.google.com/search?q=sql+select+distinct) this? – trincot Feb 14 '16 at 18:13
  • You need all the **distinct** rows? Or rows with distinct values on a single column? – Simply Me Feb 14 '16 at 18:14

1 Answers1

0

If you need only distinct rows, you can use the distinct keyword:

select distinct * from charts

However, if you need distinct values for a single column, you need to use a group by in your clause, depending what column needs distinct values and agregate the other ones.

Simply Me
  • 1,579
  • 11
  • 23
  • yes i know my problem is INSERT INTO charts (timespan, visits) VALUES "2016-02-14 10:00 PM", '1234')"; so if hit 10 time it will add to my sql table so i want no duplicate of the visits = 1234 exemple if in the insert the visits = 1234 then dont need to added is just neet function to check if the visits is already in the database – Tim Feb 14 '16 at 23:21
  • Sorry, I can't understand you very well. If you ant distinct data, just as I said in answer. ... You can also use `select avg(timespan), visits from charts group by visits` – Simply Me Feb 15 '16 at 08:33
  • @Tim, I am glad I could help you. – Simply Me Feb 15 '16 at 21:02