3

I'm looking for an SQL Statement to achieve the following:

I have a table that is like {ida | idb | count | direct}
I want to insert rows into the table taking the ids a and b from another table, count and direct are given values. I tried something like this:

Not Working:

INSERT INTO my_tbl (ida, idb, count, direct)  
SELECT id FROM other_tbl WHERE word ='test'  
UNION  
SELECT id FROM other_tbl WHERE word ='tost' 23, 5;

Thanks in advance.

5
  • is word ='tost' 23, 5 valid grammar in pssql? Commented Jul 21, 2018 at 12:58
  • 1
    Your insert columns must be match with select columns. In this sample u are trying to insert four columns with selecting one columns. Commented Jul 21, 2018 at 12:59
  • probably not, i tried to get this query working by try and error and this is where i decided to get help Commented Jul 21, 2018 at 13:02
  • @MatthiasSchroer . . . Sample data and desired results are usually a big help in conveying meaning. Commented Jul 21, 2018 at 13:04
  • @KadirÇetintaş i have some trouble getting to values from the same other_tbl with different wheres, is there a way to do this (and combine them with two given values)? Commented Jul 21, 2018 at 13:05

1 Answer 1

3

Is this what you want?

INSERT INTO my_tbl (ida, idb, count, direct) 
    SELECT o1.id, o2.id, 23, 5
    FROM other_tbl o1 JOIN
         other_tbl o2
         ON o1.word = 'test' AND o2.word = 'tost';
Sign up to request clarification or add additional context in comments.

1 Comment

yeah this is exactly what i was looking for, thanks!

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.