Plasticated
Plasticated

Reputation: 141

Getting unique values from a mySQL table between a date range

I have this table:

alt text

I need to query the table and ask the following questions:

How many rows matching uri 'x' exist with unique IP addresses between 'y' and 'z' dates.

I was experimenting with COUNTs but I couldn't seem to get the result I needed...

Upvotes: 2

Views: 696

Answers (2)

Richard Warburton
Richard Warburton

Reputation: 1472

The DISTINCT keyword enforces that only unique entries are returned in the query. As the above answer by ConroyP mentions you can nest DISTINCT within other functions in SQL. Note that this is has a different function from 'UNIQUE' which enforces uniqueness constraints in the Data Definition Language part of SQL. Some databases also allow you to substitute the keyword UNIQUE for DISTINCT in the query - but this doesn't seem to work everywhere.

Upvotes: 1

ConroyP
ConroyP

Reputation: 41906

Try using COUNT(DISTINCT())

Returns a count of the number of rows with different non-NULL expr values.

So your query would be something like

SELECT COUNT( DISTINCT( user_ip ) ) 
FROM table 
WHERE timestamp BETWEEN Y and Z

Upvotes: 5

Related Questions