MySQL select random row that has a count of 3
You can subselect the words with count(*) bigger than 2 and then pick a random selection
in the outer query
Schema (MySQL v8.0)
CREATE TABLE wordtable (
`word` VARCHAR(5),
`wordType` VARCHAR(4)
);
INSERT INTO wordtable
(`word`, `wordType`)
VALUES
('house', 'noun'),
('house', 'noun'),
('house', 'noun'),
('car', 'noun'),
('car', 'noun'),
('car', 'noun'),
('floor', 'noun'),
('floor', 'noun');
Query #1
SELECT
`word`
FROM wordtable
WHERE `word` in (
SELECT
`word`
FROM wordtable
WHERE `wordType` = 'noun'
GROUP BY `word`
HAVING count(`word`) >= 3)
ORDER BY RAND()
LIMIT 3;
word |
---|
car |
house |
car |
PHP Select random but variabeles have different weights
@Dagon is correct. Use the example from that other question. Again Dagon found it here: MySQL: Select Random Entry, but Weight Towards Certain Entries The comments are really helpful. You basically multiple that row by its weight. To add to that question and answers I would add the rows into an array and then randomly pick one (one array value) to display. When something has more weight just place it in that array times its weight.
Related Topics
PHP Json_Encode Class Private Members
PHP Output Showing Little Black Diamonds With a Question Mark
Why Is PHP Session_Destroy() Not Working
Format Bytes to Kilobytes, Megabytes, Gigabytes
Connect PHP to Mssql Via Pdo Odbc
Can You Get a Windows (Ad) Username in PHP
Warning: MySQL_Fetch_Array() Expects Parameter 1 to Be Resource, Boolean Given In
Youtube API - Extract Video ID
What Is the Maximum Length of a String in PHP
Is Possible to Keep Session Even After the Browser Is Closed
Listing All the Folders Subfolders and Files in a Directory Using PHP
Get Everything Between ≪Tag≫ and ≪/Tag≫ With PHP
Accessing Outside Variable Using Anonymous Function as Params
How to Combine Two Arrays Together