How to draw a name in a database [duplicate] - php

This question already has answers here:
How to request a random row in SQL?
(30 answers)
Closed 8 years ago.
I have names in my database and I want to draw a name for a contest.
Anyone have an idea for that ?
Thanks !!

SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1
This works in constant time, regardless of the table size, if num_value is indexed. One caveat: this assumes that num_value is equally distributed in the range 0..MAX(num_value). If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).

A query like this could work
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

Related

Selecting most recent row from a table [duplicate]

This question already has answers here:
Select last row in MySQL
(11 answers)
Closed last year.
id
first_die
24
2
25
6
How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.
Use:
select id,first_die
from test_tbl
order by id desc limit 1 ;
Demo:
Or:
select id,first_die
from test_tbl
where id = (select max(id) from test_tbl);
Demo:
If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).
If you want to return the entire recordset or row use the code below
SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);

How i can have fast random select from a big table in mysql with very Condition? [duplicate]

This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed 7 years ago.
i want adding fast random select to this query :
SELECT * FROM postlink WHERE `source`='$mysource' AND NOT EXISTS (SELECT sign FROM `state` WHERE postlink.sign = state.sign AND `cite`='$mycite') LIMIT 2
i used this but that is very slow :
SELECT * FROM postlink WHERE `source`='$mysource' AND NOT EXISTS (SELECT sign FROM `state` WHERE postlink.sign = state.sign AND `cite`='$mycite') ORDER BY RAND() LIMIT 2
note : this topic not duplicated because i want to random select with very Condition and not a simple random select.
please help.
thank you.
For this query:
SELECT pl.*
FROM postlink pl
WHERE `source` = '$mysource' AND
NOT EXISTS (SELECT 1
FROM `state` s
WHERE pl.sign = s.sign AND s.`cite` = '$mycite'
)
ORDER BY RAND()
LIMIT 2;
I would start with indexes: postlink(source, sign) and state(sign, cite). If that doesn't meet your needs, then I'd look at ways of improving the random selection.

How to check if limit was used in mysql? [duplicate]

This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 7 years ago.
In mysql query, I have something like I select and order, and use where clause, etc..., and finally I have limit 10 for example. Something like this:
Select *
From employee
where fired=0
limit 10
The problem is, how do I know if limit was used and the result was truncated? Basically I want to know easily if there were more results that got cut off from limit. I don't want to do something like
Select (count *)
From employee
where fired=0
subtract
Select (count *)
From employee
where fired=0
limit 10
Is there a simple way for this? Perhaps, like you know how after you run an insert statement, you can get the id using php built in function get_sql_id() (something like that), is there a sql_count_without_limit()?
Thanks
You could do
Select *
From employee
where fired=0
limit 10+1
So that:
0-10 values returned: no limit was reached
11 values returned: Limit was reached
And then only use the first 10 values that are returned.
update Raymond's answer is waaaaay better than mine!
You can use the found_rows function like this:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
For more information see mysql reference
You can return the remaining rows as an column like below
SET #limit = 10;
SELECT *,
(SELECT count(*) from employee)- #limit as remaining
From employee
where fired=0
limit 10

Select random rows from mysql table [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Selecting Random Rows in MySQL
I'm creating a simple web application using PHP and MySQL. In it, I need to randomly select a small set of rows from a table in a random order. How can I achieve such thing using MySQL?
SELECT * FROM table ORDER BY RAND() LIMIT 10;
Edit:
Useful information about the MySQL RAND() function can be found here.
select * from table order by rand() limit 10
Note that order by rand() with large dataset is very slow but in your case it's not a problem.
you could do that using RAND() function .
SELECT questine FROM tablename ORDER BY RAND() LIMIT 10
will select 10 questines at random under assumption the questine is stored under field questine

random select from table [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to request a random row in SQL?
I have table:
name,age,school
jane,15,zu
peter,16,zu
john,15,stu
Tomas,15,kul
viera,17,stu
tibor15,zu
I want select from this table 1 person (randomly) per school
select * from table group by school order by rand()
It's terribly innefficient since it's ORDERing the entire table randomly, and then GROUPing on a potentially huge unindexed temporary table, but this works.
SELECT *
FROM (
SELECT *
FROM my_table
ORDER BY RAND()
)a
GROUP BY school
It would likely be more wise to break it down.
One query to retrieve a list of
schools.
For each school, one query to get a random student for that school.
See this post for some slick tricks on how to get single random values efficiently.
If you are using php (as the tag suggests), run SELECT * FROM table; then generate a random number based on the number of results in the query. For example:
i = floor(random()*query.length);
Then seek to the record indicated seek(i) and viola, you have a random entry =)
You'll have to use your own knowledge/documentation for the exact syntax, I haven't touched PHP in a while, but the logic is sound.
Gary

Categories