knex update multiple rows in one query - sql

Is it possible to update multiple rows with one query? Like in insert i can pass an array of objects and each key refers each column. Is there anything like that for an update query?
I have an array of objects (id, value) and i want to update all the fields that match id from the object with the value from the same object.

There is a way to update it using PostgreSQL query and it was answered here. But it requires some magic and ugly .raw code to use this with knex. So, I'd recommend using multiple update statements in one transaction. And synchronize them using Promise.all([updates]).

Related

Can I prevent duplicate data in bigquery?

I'm playing with BQ and I create a table and inserted some data. I reinserted it and it created duplicates. I'm sure I'm missing something, but is there something I can do to ignore it if the data exists in the table?
My use case is I get a stream of data from various clients and sometimes their data will include some data they previously already sent(I have no control on them submitting).
Is there a way to prevent duplicates when certain conditions are met? The easy one is if the entire data is the same but also if certain columns are present?
It's difficult to answer your question without a clear idea of the table structure, but it feels like you could be interested in the MERGE statement: ref here.
With this DML statement you can perform a mix of INSERT, UPDATE, and DELETE statements, hence do exactly what you are describing.

Elegant way to replace a single record on a very small table?

I have a small table with only a few records (~20) that I'm using as cache for a really expensive query that returns an array of strings. The table contains a single text column and it will be updated every few minutes. The array is updated in memory and most of the time only one of the records is changed.
I'm thinking of using a transaction with something like:
INSERT INTO cache(id)
SELECT unnest($1::text[])
ON CONFLICT DO NOTHING
DELETE FROM cache
WHERE id NOT IN (SELECT unnest($1::text[]))
but I have a feeling that I might as well just delete everything and then insert it again since it's such a small table. Another option would be to try and combine the queries with a CTE or something. What's the best practice?
Thanks!

How do I use sets in SQL?

In case you're not clear what a set is it allows a user to insert a value, check if a value is in the list and find an intersect (if value exist in two sets). Also a set will only hold a value once.
How do I use a set in SQL? I'm using sqlite but will be moving to postgresql. The most obvious option to me was have a Set table of SetID, SetValue with those two values being the primary key. However I have a feeling this wouldn't be efficient as SetID would be repeated many times. The other option is to have a table int SetId, blob SetArray but I'll need to implement set logic by hand.
What's the best way to do sets in SQL? For either sqlite or postgresql?
A couple options:
Trigger to see of value exists and if it does, return NULL (accomplishes exactly what you are doing)
Just use unique constraints and handle errors
Both these move the table from bags to sets but they do so in different ways, one by giving it more select-like semantics (for a slight performance cost) and the other, more traditional SQL way, to force data validation on entry.
If you want to select as a set, use select distinct instead.

How do I get count(*) from table where cond1=$cond1 and cond2=$cond2 real-time

guys:
Assuming that I have a base table, which records tuples. If users want to get the count(*) satisfying some conditions, they can use SQL query like this:
SELECT count(*) FROM table where cond1=$cond1 AND cond2 = $cond2 AND...
Question 1: If the condition keeps the same, how can we get the real-time count? for some reason that I can not use count(*) directly to fullfill the task.
Question 2: If new condition occurs, how to extend the case in question 1?
Although it's hard to imagine what exactly can prevent you from using COUNT() one possible way of achieving your goal (if I understand your requirements correctly) and assuming that number of possible combination of parameters are limited, might be:
Creating a table that keeps counts for different sets of conditions
Creating trigger(s) on the base table that repopulate(s)/recalculate(s) count values on update, insert, delete operations
Reading counts from that table
Update your triggers

select, delete & update from multible tables using a single query in mysql

for example i use following command to find a record
SELECT `users`.`mail`
FROM `users`
WHERE `users`.`uid` = %s
if found an match, then i should like to delete this record, and update in the same query an another table. i can solve this with 'joins' ?
if found an match, then i should like to delete this record, and update in the same query an another table. i can solve this with 'joins' ?
Not with a single SQL query, no.
But you could perform those actions, using separate SQL queries, within a single stored procedure. This would be faster than submitting three queries separately from your application, because there's no time/performance lost transferring data back and forth over the wire (to and from your application code).
No, there's no way to do three separate operations in one query.
Why do you need to do it in one?
If your goal is to improve performance you can do it using a single CALL to the DB using a store procedure that does 2 different queries inside.
I think the only reason you would want to select, then delete, is if you are doing it yourself and want to make sure you are deleting the right things. Something like
DELETE users.mail FROM users WHERE users.uid = %s
will return 0 if it deleted no rows, or a number of rows it deleted. You could just check the return and make a decision based on that as to what to update...