随机混合两个mysql表

时间:2014-05-27 19:30:07

标签: php mysql sql

这个概念非常简单,但我需要帮助,因为无法做到这一点。

我有2张桌子

name   last       hours
Mario  Rossi      8
Mario  Bianchi    8
Mario  Galli      4
Luigi  Masso      4

和时间

in      out               tot
08.00   16.00             8
09.00   17.00             8
10.00   18.00             8
11.00   19.00             8
08.00   12.00             4
09.00   13.00             4
10.00   14.00             4
11.00   15.00             4

我想要这个输出:

Mario Rossi: random in/random out    
Mario Bianchi: random in/random out 
Mario Galli: random in/random out
Luigi Masso: random in/random out 

在join hours = tot tables

的范围内

1 个答案:

答案 0 :(得分:1)

以下是一种选择适当的随机in时间然后计算out时间的方法:

select t1.name, t1.last,
       (select in
        from table2 t2
        where t2.tot = t1.hours
        order by rand()
        limit 1
      ) as randomIn,
      addtime(randomin, t1.hours) as randomOut
from table1 t1;
相关问题