I'm facing a problem with a voting system specially in the vote up feature.
How can I make something like votes++ in the database without the need to select the last votes in single query?
UPDATE myTable
SET voteCol = voteCol + 1
WHERE id = idOfInterest;
Related
I would like to know how to loop through each record in a database, and update them using a counter.
For instance, I have a large number of email fields. I would like to update them all using the pattern: 'hello+1#gmail.com', 'hello+2#gmail.com', 'hello+3#gmail.com', 'hello+4#gmail.com' ...
I tried to do this without SQL in my Rails console, but it would take too much time. Any suggestions on how to do this using SQL?
If you are using SQL server:
select <stuff>
,row_number() over (order by <something>) as row_number
from <things>
where <something>
Will do the trick. If not, let us know what you are using and we can go from there! Are you looking to put the '+Row_number' before the #? If so it should be a fairly simple matter of replacing the the '#' with '+4#' and so on.
In MySQL, you would use variables:
update table t
set email = concat('hello+', #rn := coalesce(#rn, 0) + 1, '#gmail.com');
If you have a particular ordering in mind, you can add an order by clause.
I am working on a vocable trainer written in python and using a SQLite database.
I have a function where the user can change existing vocables. With the following sqlite syntax I can choose a specific row, in this case row 8 from table Table_1
SELECT * FROM 'Table_1' LIMIT 1 OFFSET '8';
The only way I knwo to update rows is the following:
UPDATE 'Table_1' SET answer='bla' WHERE question='blub';
I do not store the ID number of the vocable in a column, I just store question, answer, phase, next_day. Is it possible to update a sepecific row without using this WHERE question 'blub' syntax but using a syntax like:
UPDATE 'Table_1' SET answer='bla' WHERE LIMIT 1 OFFSET '8';
This one is actually not working, but I guess you know what I mean.
I'm looking for a possible solution to the following. I have data stored in a table to keep track of a special increment number the customer wants in the DB. This is a special number they use internally.
What I would like to do is automatically increment this number in the table when I select it. So I don't have the problem of another transaction, from someone else using the system, using the same ID number.
So I want to select the current number and increment it by one at once so I don't have duplicates. How would I go about doing this if it is even possible?
UPDATE the_table
SET the_column = the_column + 1
WHERE qualifier = X
RETURNING the_column;
This ought to do the trick, with the caveat that it will return the new id rather than the old one:
UPDATE foo
SET id=nextval('foo_sequence')
WHERE ...
RETURNING *
I am trying to update my current table by drawing data from another table.
My database (dbo_finance)
column - test
The other database is assestsc and I am going to pull the data from column issuename1,
however I only want to pull the issuename1 when the field [MinSecClass] is = 9.
This is what I wrote
UPDATE dbo_finance
SET [dbo_finance].cusip9 = AssetsC.cusip
FROM dbo_finance INNER JOIN AssetsC ON dbo_finance.test = AssetsC.[IssueName1]
WHERE (AssetsC.MinSecClass = 9)
Thanks, first time really using SQL
Well I would use aliases, it's a good habit to get into:
UPDATE f
SET [dbo_finance].cusip9 = AssetsC.cusip
FROM dbo_finance f
INNER JOIN AssetsC a ON f.test = a.[IssueName1]
WHERE (a.MinSecClass = 9)
Now that will work fine if the assets table will only return one value for cuspid for each record. If this is a one to many relationship you may need to get more complex to truly get the answer you want.
I see several serious design flaws in your table structure. First joins fields that are dependant as something as inherently unstable as issue name are a very poor choice. You want PK and FK field to be unchanging. Use surrogate keys instead and a unique index.
The fact that you have a field called cusp9 indicates to me that you are denormalizing the data. Do you really need to do this? Do you undestand that this update will have to run in a trigger ever time the cuspid assoicated with MinSecClass changes? Whya re you denormalizing? Do you currently have performance problems? A denormalized table like this can be much more difficult to query when you need data from several of these numbered fields. Since you already have the data in the assets table what are you gaining except a maintenance nightmare by duplicating it?
UPDATE dbo_finance
SET cusip9 = (
SELECT A1.cusip
FROM AssetsC AS A1
WHERE dbo_finance.test = A1.IssueName1
AND AssetsC.MinSecClass = 9
)
WHERE EXISTS (
SELECT *
FROM AssetsC AS A1
WHERE dbo_finance.test = A1.IssueName1
AND A1.MinSecClass = 9
);
Because you're using SQL 2008, you can take advantage of the new(ish) MERGE statement.
MERGE INTO dbo_finance
USING (SELECT IssueName1, cusip FROM AssetsC WHERE MinSecClass = 9) AS source
ON dbo_finance.test = source.IssueName1
WHEN MATCHED THEN UPDATE SET dbo_finance.cusip9 = source.cusip;
I'm working on a simple todo app that has a column for each day of the week. On startup, the app checks to see if there are any incomplete tasks from before the current date. If so, they're moved to the top of the current date's column.
The app is cloud-based, but the tasks are backed up for offline mode with an SQLite db. I can easily move the tasks by updating their date property, but I need the order property of each task to increment starting at 0 to place them at the top.
I need to be able to define a count variable in SQLite alone and increment it with each update that's performed. I know this code doesn't work, but it's an easy way of explaining what needs to be done:
count = 0
UPDATE `tasks`
SET `date` = '2010-01-04',
`order` = `count`++
WHERE `date` < '2010-01-04'
I'd rather not use a temporary table or use a counter outside of SQLite, if possible.
#OMG Ponies - I'm starting to think it's just not possible all in SQLite.
I would do it with a second SQL-Statement:
UPDATE `tasks`
SET `date` = '2010-01-04',
count = ( SELECT COUNT(`task_id`) FROM `tasks` ) + 1
WHERE `date` < '2010-01-04'
Not tested but should work!
SQLite is a great tool, but I'd be surprised if this was possible using JUST SQLite. T-SQL support isn't that great within that app.
You could probably update the existing value in the record; adding or subtracting from it, but that would require the records to already be in order.
Actually, you can use addition in Sqlite - check out http://www.sqlite.org/lang_expr.html ; you just need to use an extra UPDATE statement to increment the counter and an inner query to get it again. Make sure to do this all in a Transaction or else you'll get concurrency problems.
it sounds to me that you'd like a a count column for each row. If i were you, i think you could do the call as
UPDATE `tasks`
SET
`date` = '2010-01-04',
count = count + 1
WHERE `date` < '2010-01-04'
lemme know how that works...