Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
-1 votes
2 answers
565 views

A quick way to ORDER all IDs randomly in MySQL [closed]

I need to assign random IDs to a table. Thus, I create a mapping table as CREATE TABLE t2 ( ID int(11) unsigned NOT NULL AUTO_INCREMENT, SourceID int(11) unsigned NOT NULL, UNIQUE INDEX(SourceID), ...
Googlebot's user avatar
  • 4,531
0 votes
1 answer
711 views

MySQL order by rand but result must exist different specific value

I'm trying the get random values from SQL but I want results must have minimum one specific value from one column, how can I do it? For example; TABLE; id | category | name ---------------------...
Topup's user avatar
  • 13
2 votes
2 answers
224 views

Select random database records between subset of records having the same count(*) in a query

I have this simple query: SELECT `value`, count(*) as `noHits` FROM `topic` WHERE identifier IN ('" . implode("','", array_keys($processedIdentifier)) ."') GROUP BY `value` ...
Ola's user avatar
  • 21
1 vote
1 answer
4k views

How can I create a unique and random alphanumeric string using MySQL's Default Value? (v 5.6)

I am building a MySQL database and think using implicit DEFAULTS seems very promising. I am successfully using DEFAULT CURRENT_TIMESTAMP and DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP for ...
LWSChad's user avatar
  • 111
-1 votes
1 answer
562 views

Randomly select 4 items of a randomly selected category

I need an idea how to construct this query. I have a web page. each time the page is refreshed, I want to select data from a random category, and to select no more than 4 random items from that ...
david's user avatar
  • 1
2 votes
3 answers
9k views

how to replace user's names with random names?

How can I use SQL only on mysql DB to replace first and last names of real users in users table using a random name from two related tables: random_first_names and random_last_names. Our users table ...
user1525248's user avatar
0 votes
2 answers
1k views

Fetch one value from random row where value is not x

I have a games table in my db, which has id column(pk) and slug column(unique) - other columns are not relevant. Now, what I'd like to get is one row, or it's slug value only, which would be random ...
errorous's user avatar
  • 103
1 vote
2 answers
10k views

how to select random and unique records from mysql (in php language)

I have a question table in my database which consists the following columns question number which ranges from 1 to 150. question text text for 4 options as A,B,C,and D answer option(either a,b,c or ...
Pankaj Agrawal's user avatar
1 vote
2 answers
4k views

How to select random row with count()

Table names: g_number name 123 John 123 Sara 123 Michael I want to get one RANDOM name and quantity of all names with the same g_number in one query: SELECT name, count(*) cnt ...
Igor's user avatar
  • 121
1 vote
2 answers
84 views

How we can design best optimized solution: Generate huge random numbers and check in database table whether this numbers are there or not

We have one table like: Table Name: random_number_series Its columns are as below: number [numeric(10)] name [varchar(10)] status [tinyint(1)] type [tinyint(1)] ...
murtaza.webdev's user avatar
1 vote
1 answer
3k views

How Can Get Random 2 rows FROM EACH GROUP FROM JOINED TABLE

EXAMPLE Table Dept: dept_id dept_name dept_status --------------------------------------- 1 Sales 1 2 Develpment 1 3 HR 1 ...
rva's user avatar
  • 13
2 votes
2 answers
1k views

Selecting random rows from database initially while preserving that order later

I am building a quiz app, in which the student can login and can select the option of give exam. I want to show multiple choice questions to students randomly. I stored all the questions in one ...
user3263553's user avatar
1 vote
1 answer
805 views

mysql - select random rows with max 3 rows per parent id

I currently have a query that returns a maximum of 3 rows per parent_id. I want to enhance the query to return a maximum of 3 random rows per parent_id. This does not work for me at the moment as it ...
nimrodma's user avatar
  • 111
0 votes
1 answer
893 views

Result occurs often with fast random select query

I have this query to display 4 random photos on a website (I have it from here (when I asked for something faster than ORDER BY RAND())): (SELECT `p1`.`id`, `p1`.`url`, `p1`.`hits`, `p1`.`...
haheute's user avatar
  • 161
1 vote
1 answer
1k views

What is the most efficient way to exclude records with a particular ID in a MySQL query for random records?

I have an api endpoint that returns X amount of random posts from a table called "posts". This endpoint is used by a mobile app. It retreives random posts by doing a SELECT COUNT(*) on the posts table ...
Adé's user avatar
  • 119
4 votes
3 answers
8k views

Select random 10 unique rows from MySQL database?

I have one table (questions) with 54 questions. The students have to answer all 54 questions. I have to show 10 questions at once, then next 10 and so on.... I have to select 10 questions ...
Bikram Pahi's user avatar
9 votes
2 answers
18k views

Randomizing the Auto Increment in Mysql

We have a database, where we insert data into table whenever we receive an order from our services. We use auto_increment to generate the ID and assign the order with the same. This is usually same ...
RaceBase's user avatar
  • 203
1 vote
2 answers
2k views

Perform group by with random group expression (randomly partitioning rows) MYSQL

Let's say I have a table (with millions of rows) cat1|cat2|value A | B| 10 ... and I want to group the data by cat1 and also split the grouping into 10 (at least roughly) equal partitions of ...
seanv507's user avatar
  • 111
0 votes
1 answer
1k views

Getting MySQL User Variable in Sequence, While Using ORDER BY RAND()

I am running a SELECT statement, which ultimately will be in an INSERT..SELECT statement. I want a result like this ID COUNTER 3 1 7 2 1 3 20 4 12 ...
Chris's user avatar
  • 113
0 votes
1 answer
1k views

MySQL non-sequential primary key of certain length

what's the best way to create a non-sequential primary key of a certain length in MySQL? I'm looking to use the primary key as a 'Pin' number which real actual human people can use to refer to the ...
Josh's user avatar
  • 103
8 votes
4 answers
7k views

How do I do a complex GROUP BY in MySQL?

I have a table that contains several keys into other tables (where each key is comprised of multiple columns). I would like to be able to group rows together that have an equal key, but I don't want ...
Michael McGowan's user avatar
5 votes
1 answer
6k views

How do I make MySQL auto increment in random order?

I have a table that contains several keys into other tables (where each key is comprised of multiple columns). I would like to create a new column for each key that would be an integer such that ...
Michael McGowan's user avatar