question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Are inserts of values of a sub-select supported?

See original GitHub issue

Hi,

I’m trying to build a INSERT query that would use values of a sub-query:

INSERT INTO entries (secret, sequence) 
VALUES ('123', SELECT( COUNT(*) FROM entries WHERE secret='123'))

Passing a builder as a value of the sequence field does not seem to do the job:

knex('entries').insert({
  secret: '123', 
  sequence: knex('entries').select().where('secret', '123')
}).then(...);

Documentation does not mention this case either. Is it supported?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
luigycommented, Dec 6, 2013

for now you can do

knex('entries').insert({
  secret: '123', 
  sequence: knex.raw("(select count(*) from entries where secret = '123')")
}).then(...);

but, I have a patch to support a builder. Thoughts @tgriesser? Also what was the reason for using clone() in toString()?

0reactions
tgriessercommented, Jun 4, 2014

Fairly certain this is covered in 0.6 - let me know if that’s not the case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I insert values into a table, using a subquery with ...
I have two tables in SQL Server: article; prices. Now I want to select a certain set of ids and insert some entries...
Read more >
How to Use Subqueries in INSERT, UPDATE, and DELETE ...
Do you find subqueries useful in SELECT? Learn how to use them in INSERT, UPDATE, and DELETE and see how powerful your SQL...
Read more >
INSERT with SUBQUERY in VALUES clause — oracle-mosc
i am reading everywhere that subquery cant be used in the value clause of an insert statement .
Read more >
Are inserts of values of a sub-select supported? #121 - GitHub
Hi, I'm trying to build a INSERT query that would use values of a sub-query: INSERT INTO entries (secret, sequence) VALUES ('123', ...
Read more >
Inserting records using subqueries - w3resource
In this page we are discussing, how to insert rows using INSERT INTO statement, where rows are results of a subquery, made up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found