Welcome Guest, Not a member yet? Register   Sign In
select twice from one table
#1

[eluser]megabyte[/eluser]
I want to know if i can save some php timeand do this straight in the db.

What i want to do, is do 2 selects from the same table then insert it into another table.

I know there is "select insert" but not sure how to do it here.


// run all three queries below as one

select from lessons where course_id = 1 ORDER BY RAND() LIMIT 3;

select from lessons where course_id = 3 ORDER BY RAND() LIMIT 5;

select insert into users_lessons
#2

[eluser]Phil Sturgeon[/eluser]
I would be tempted to head down the UNISON line with this one.

Quote:INSERT INTO users_lessons ul
(
SELECT l1.field1, l1.field2, l1.field3
FROM lessons l1 WHERE course_id = 1 ORDER BY RAND() LIMIT 3
)
UNISON
(
SELECT l2.field1, l2.field2, l2.field3
FROM lessons l2 WHERE course_id = 3 ORDER BY RAND() LIMIT 3
)

That is just some theoretical code. Not been tested.




Theme © iAndrew 2016 - Forum software by © MyBB