PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 1, 2022

[FIXED] MySQL random id restricted by column

 February 01, 2022     mysql, php, phpmyadmin     No comments   

Issue

I have a table with id's ordered from 1 to 300 which have more columns (A,B,C,D,etc)

How to get a random id restricted by the info in column C for example

Example

ID   column C
1    teacher
2    student
3    teacher
4    student
etc...

Imagine i only want id random from "students" in column C (in this example it can only be 2 or 4)

Thanks


Solution

Here is one way:

select id
from table t
where c = 'student'
order by rand()
limit 1;

With only 300 rows in the table, the performance should be fine.



Answered By - Gordon Linoff
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing