user1816133
user1816133

Reputation:

How to select data from database higher than exact number

I need to select users from database, which score (column named is time) is higher than 10. How to do that? For now It selecting all users.

For now my code:

    $query = "SELECT userName,min(time) time FROM game group by userName order by time ASC LIMIT 15";
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);

Upvotes: 0

Views: 145

Answers (4)

flavien317
flavien317

Reputation: 48

You need do:

SELECT userName,min(time) time 
FROM game WHERE time > 10 
group by userName 
order by time ASC LIMIT 15;

Upvotes: 1

Racil Hilan
Racil Hilan

Reputation: 25351

Add HAVING MIN(time) >10 to your query like this:

$query = "SELECT userName, MIN(time) time FROM game GROUP BY username HAVING MIN(time) >10 ORDER BY time ASC LIMIT 15";

Your query will look like this:

SELECT userName, MIN(time) time 
FROM game
GROUP BY userName 
HAVING MIN(time) >10
ORDER BY time ASC LIMIT 15 

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

You can use WHERE clause to filter your result

SELECT userName,min(time) time 
FROM game
WHERE  `time` >10
group by userName 
order by time ASC LIMIT 15

or if you want to filter on resultset of aggregate function you can use HAVING clause

SELECT userName,min(time) time 
FROM game
group by userName 
HAVING  min(time) >10
order by time ASC LIMIT 15 

Upvotes: 3

dkasipovic
dkasipovic

Reputation: 6120

Just add WHERE clause, like:

    $query = "SELECT userName,min(time) time FROM game WHERE time>10 group by userName order by time ASC LIMIT 15";
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);

Upvotes: 1

Related Questions