where clause - mysql select: randomly select with another criteria if not met the max. number of rows -
i need select table under criteria limit of 20 randomly. used next same
select *, rand() table column1 '%text%' order rand() limit 20
but criteria wont meet , less 20 rows selected . in such cases needed add together select statement in there criteria , not equal previous selection (i mean distinct) how can accomplish single query (something conditional query).
mytable defines
create table if not exists `mytable`( `id` int(11) not null auto_increment, `name` varchar(100) not null, `house` varchar(100) not null, `place` varchar(100) not null, primary key (`vinrav_examz_id`) );
content of table
id------name-------house------place-- 1-------data1------data2------data3-- 2-------data4------data5------data6-- 3-------data7------data8------data6-- 4-------data9------data9------data10-- 5-------data11-----data8------data12--
then first query
select *, rand() mytable palce 'data6' order rand() limit 20
which give 2 rows < 20.
2-------data4------data5------data6-- 3-------data7------data8------data6--
in case execute
select *, rand() mytable house 'data8' order rand() limit 20
which return
3-------data7------data8------data6-- 5-------data11-----data8------data12--
then combined need get
2-------data4------data5------data6-- 3-------data7------data8------data6-- 5-------data11-----data8------data12--
thankz in advance
select * table -- column1 '%text%' -- (notice removed where) order case when column1 '%text%' 1 when column1 '%secondcriteria%' 2 when column1 '%thirdcriteria%' 3 else 4 end, rand() limit 20
select where-clause
No comments:
Post a Comment