I want to ask something. I don't understand how to get same data from table at only one time. Here are the example

Like table name product_data and there are atleast six field.

in data field the date look like this

product Date
23-04-2012
23-04-2012
23-04-2012
23-04-2012
23-04-2012
23-04-2012
18-04-2012
18-04-2012

But i want the result look like this.

23-04-2012
18-04-2012
I only want to fetch the same date field in only one time. So anyone can tell me the solution.

Thanks,

Dani AI

Generated

: and are correct that SQL can return each date only once; ’s perf note is also useful. A few practical points that weren't mentioned above and will matter later:

If the column includes a time part (DATETIME/TIMESTAMP) then rows with the same calendar day can still be different. Collapse the time to the calendar date in the query, dedupe on that result, and optionally sort. For example, extract the date portion and group/order by it:

SELECT DATE(`date`) AS day
FROM product_data
GROUP BY day
ORDER BY day DESC;

If the column is currently a text field (not a DATE), two strings that look identical may hide formatting differences. Convert and store dates using a proper DATE type (or use STR_TO_DATE on import) so comparisons are reliable.

Performance note: wrapping the column in a function prevents use of a plain index. On large tables, keep a DATE-only column (maintained on insert/update) or add a generated column (MySQL 5.7+) and index that column so queries like the one above are fast. Example to add a stored generated column and index it:

ALTER TABLE product_data
  ADD COLUMN date_only DATE GENERATED ALWAYS AS (DATE(`date`)) STORED,
  ADD INDEX idx_date_only (date_only);

Final cautions: avoid using generic names like date without backticks, prefer ISO date storage (YYYY-MM-DD), and decide whether you just need the distinct date list or representative rows per date—if you need rows, you will have to pick which row (first/last) per date and use aggregation or a join/window function.

Recommended Answers

All 4 Replies

Try this query: SELECT DISTINCT date FROM product_data.

thanks

An alternative is the group by query
SELECT date FROM product_data GROUP BY date

Please mark this post as solved by the way

Member Avatar for Member #120589

DISTINCT will be quicker if the 'date' field is not indexed. However, I would imagine that the speed benefit would be minimal for a simple query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.