php - mix random with two mysql tables

109

The concept is very simple, but I need help because cannot make this work.

I have 2 tables

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

and the times

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

I want this output:

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

in the range of the join hours=tot tables

117

Answer

Solution:

Here is one method that selects an appropriate randomin time and then calculates theout time:

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;

People are also looking for solutions to the problem: php - laravel 4 - wrong id out of 2 id's within the routes

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.