1

I'm trying to insert data from one table into another. I would like to put the info from unit_66 in one column and the info for unit_166 into another column but I'm having trouble using two SELECT statements, I get this error when executing the code:

[Err] 1242 - Subquery returns more than 1 row

CREATE TABLE time_test (unit_66 BIGINT, unit_116 BIGINT);

insert into time_test (unit_66, unit_116)
VALUES 
(
(select time_stamp from `events` where unit_id = 66 LIMIT 50),

(select time_stamp from `events` where unit_id = 116 LIMIT 50)
);

Can anyone tell what the problem it is?

5
  • 1
    Why are you using limit 50? Commented Jun 8, 2014 at 19:23
  • Time_stamp is huge im just limiting it to 50 rows until i can get it to work. Commented Jun 8, 2014 at 19:25
  • That is your problem, the way it is written, it expects one value, not 50 rows of values. Commented Jun 8, 2014 at 19:25
  • how do i properly limit it then? Commented Jun 8, 2014 at 19:28
  • Gordon answered it for you. Commented Jun 8, 2014 at 19:28

3 Answers 3

4

I think you want insert . . . select. You are using a scalar subquery, but having it return up to 50 values. That is causing your error. If you had a limit 1 in each subquery, then it would work.

It is a little hard to guess what you are trying to do, but let me try:

insert into time_test(unit_66, unit_116)
    select e66.time_stamp, e116.timestamp
    from events e66 join
         events e116
         on e66.unit_id = 66 and e116.unit_id = 116
    order by rand()
    limit 50;

This is just a guess, because the intention of your query is not obvious to me.

Sign up to request clarification or add additional context in comments.

4 Comments

Why are you doing an order by rand()?
There's no order by in the original query. This will randomize the order explicitly. More importantly, it will make it less likely that the same value from e66 will appear multiple times in the results.
@GordonLinoff The more proper way is to loop a procedure which inserts the records with count of number of rows in the events table as looping limit.
I'm new to MySQL and im not 100% sure how this code works but when i try compiling it, gives me this error.[Err] 1052 - Column 'time_stamp' in field list is ambiguous
0

I suppose your time_test table is not normalized. So, you can use INSERT INTO SELECT statement. With this solution you will copy 100 records from events table.

INSERT INTO time_test (unit_66)
SELECT time_stamp
FROM events
WHERE unit_id = 66 LIMIT 50

INSERT INTO time_test (unit_116)
SELECT time_stamp
FROM events
WHERE unit_id = 116 LIMIT 50

1 Comment

That will insert two different rows, one with a blank unit_116 and one with a blank unit_66.
-2

insert into time_test (&unit_66, &unit_116) VALUES ( (select time_stamp from events where unit_id = 66 LIMIT 50), (select time_stamp from events where unit_id = 116 LIMIT 50) ); just try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.