1

我有一张这样的桌子。

Name    c1  c2  c3  c4

one      1   2   3  4 
one      3   1   7  6
one      5   9   2  5
one      2   5   1  8
two      1   4   2  6
two  etc.....

我想随机选择一个条件名称等于一的列。

结果应该是这样的..

c1 (This column will selected randomly)
1
3
5
2
4

4 回答 4

3

简单的怎么样,

 select <col_name> from <table_name> where name = 'one';
于 2013-02-06T06:38:24.053 回答
1
      SELECT <col_name> FROM <table_name> WHERE name = 'one' ORDER BY RAND() Limit 1
于 2013-02-06T06:48:03.367 回答
1
select <col_name> from <table_name> where name = 'one' order by newid();

newid() 函数调用的顺序将有助于随机化返回的行。

于 2013-02-06T06:45:07.357 回答
0

Since I don't know your DBMS, here is a DBMS agnostic query that will select randomly a column from your table:

SELECT
   CASE round(3*rand()+1)
      WHEN 1 THEN c1
      WHEN 2 THEN c2
      WHEN 3 THEN c3
      WHEN 4 THEN c4 
   END;
FROM
   <table_name>
WHERE 
   name = 'one'

where

round() is function that rounds its argument.

round(1.23) = 1
round(-1.58) = -2

and

rand() is a function that generates a random value v in the range 0 <= v < 1.0.

rand() => 0.123687466
rand() => 0.996574545
于 2013-02-06T09:49:41.610 回答