![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
What would the SQL statement be to randomly select 30% of the records in table2 and calculate column X equal to string 'test' for just those random records? I just have no idea how to get the random set of records. How? Thanks for any help! |
#3
| |||
| |||
|
|
What would the SQL statement be to randomly select 30% of the records in table2 and calculate column X equal to string 'test' for just those random records? I just have no idea how to get the random set of records. How? Thanks for any help! |
#4
| |||
| |||
|
|
noydb wrote: What would the SQL statement be to randomly select 30% of the records in table2 and calculate column X equal to string 'test' for just those random records? I just have no idea how to get the random set of records. How? Thanks for any help! This gives you the randomness: select * from table order by rand() really? |
|
id | +------+ 399 | 1899 | 1041 | 516 | 1510 | 2212 | 1461 | 1177 | 3137 | 1439 | +------+ |
|
size | +------+ 1049 | +------+ |
#5
| |||
| |||
|
|
the problem is that whilst establishing the number of 30% rows is easy enough.. mysql> (select floor(count(id)*0.3) as size from day); +------+ | size | +------+ | 1049 | +------+ ..mysql wont take the output from a subquery as an argument t limit.. mysql> select id from day order by rand() limit (select floor(count(id)*0.3) as size from day); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select floor(count(id)*0.3) as size from day)' at line 1 mysql |
#6
| |||
| |||
|
|
The Natural Philosopher wrote: the problem is that whilst establishing the number of 30% rows is easy enough.. mysql> (select floor(count(id)*0.3) as size from day); +------+ | size | +------+ | 1049 | +------+ ..mysql wont take the output from a subquery as an argument to limit.. mysql> select id from day order by rand() limit (select floor(count(id)*0.3) as size from day); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select floor(count(id)*0.3) as size from day)' at line 1 mysql Yeah, you are so right about that, I even tried with a CREATE FUNCTION to make it to work, but it didn't like my limit with a variable. sounds like you need to do it in two stages using a higher level |
#7
| |||
| |||
|
|
J.O. Aho wrote: The Natural Philosopher wrote: the problem is that whilst establishing the number of 30% rows is easy enough.. mysql> (select floor(count(id)*0.3) as size from day); +------+ | size | +------+ | 1049 | +------+ ..mysql wont take the output from a subquery as an argument to limit.. mysql> select id from day order by rand() limit (select floor(count(id)*0.3) as size from day); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select floor(count(id)*0.3) as size from day)' at line 1 mysql Yeah, you are so right about that, I even tried with a CREATE FUNCTION to make it to work, but it didn't like my limit with a variable. sounds like you need to do it in two stages using a higher level language, then. |
![]() |
| Thread Tools | |
| Display Modes | |
| |