Add up value using database field value with savefield in cakephp - sql

my question is pretty simple but hard to find an answer for though search engines.
I simply want to update a field in the database, using that fields old value to add another value. I'm using the following at the moment:
$this->Advertisement->saveField('total_views', '(total_views + 1)', false);
But this gives me the next query:
UPDATE `advertisement` SET `total_views` = '(total_views +1)', `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16
This is wrong and it should be:
UPDATE `advertisement` SET `total_views` = (total_views +1), `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16
The problem is where it puts (total_views +1) between quotes.
Does anyone have an idea on how to get this working?

$this->Advertisement->updateAll(
array('Advertisement.total_views' => 'Advertisement.total_views + 1'),
array('Advertisement.id' => 1)
);

Just in case anyone else gets stuck with this, the spaces in the query are important.
works: Advertisement.total_views + 1
does not work: Advertisement.total_views+1

$this->Advertisement->updateAll(array('Advertisement.total_views'=>'Advertisement.total_views+1'), array('Advertisement.id'=>$id));

I found that for a similar issue I was able to resolve it in the following manner,
$this->Advertisement->updateAll(
array('Advertisement.total_views = Advertisement.total_views + 1'),
array('Advertisement.id' => 1)
);

Related

I need help in filtering the content of this SQL column

I need help in filtering the content of this SQL column. I have unfortunately been unsuccessful so far. I will be happy for any assistance.
My goal is for all the unc paths to bear the same format.
All should look like: \\ps9\wa033242. Meaning all should begin with the "\\" replacing the "///"
I tried truncating it but because of the different string length, I have problems.
I tried truncating and UPDATING
SELECT
cw_platz.nummer,
cw_platz.nwaddress,
cw_platz.bezeichnung,
os_cw.cw_ldzuplatz.ldruckernr,
os_cw.cw_ldzuplatz.papierschacht,
os_cw.cw_ldzuplatz.treibername,
cw_logischerdrucker.bezeichnung
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung in cw_platz.bezeichnung
This is my result:
My first thought is to simply use something like REPLACE(yourstringhere, '/','\').
Is it something you already tried?
Reference: https://learn.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-2017
you could use replace
select replace('client/ps9///wa033242//', '/' ,'\');.
and for update
update your_table
set your_column = replace(your_column, '/' ,'\')
try avoid .. the where like
UPDATE os_cw.cw_ldzuplatz
SET os_cw.cw_ldzuplatz.treibername = REPLACE(os_cw.cw_ldzuplatz.treibername, '/' ,'\')
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung = cw_platz.bezeichnung
Try this it will help you.
UPDATE os_cw.cw_ldzuplatz
SET os_cw.cw_ldzuplatz.treibername = REPLACE(os_cw.cw_ldzuplatz.treibername, '\\','///')
FROM
cw_platz,
os_cw.cw_ldzuplatz,
cw_logischerdrucker
WHERE
cw_platz.nummer = os_cw.cw_ldzuplatz.platznr and
cw_logischerdrucker.nummer = os_cw.cw_ldzuplatz.ldruckernr and
cw_platz.bezeichnung = cw_platz.bezeichnung
and TREIBERNAME like '\\%'

How to use If statement in addCondition, YII

I have some conditions to hand on to my dataprovider. Now, my last condition I want only to take place only when the field "edit" is set to true -> In this case I want to check if the editConfirmed field 'editBevestigd' is set to true. If the 'edit' field is empty I don't want to add this last condition.
$criteria->addCondition('bevestigd = 1');
$criteria->addCondition('IF(edit = 1) editBevestigd = 1');
What is the best way to handle this. Can I do this in YII (problem here is that the record is not known yet). Or how do I write this in SQL (I know this last condition isn't right right now..)?
Thanks in advance!
If i right understand what you want then you should use this condition:
WHERE (edit = 1 AND editBevestigd = 1) OR edit = 0
Thus, the condition becomes:
$criteria->addCondition('(edit = 1 AND editBevestigd = 1) OR edit = 0');
You can try like that
//for php variable set or not
if($edit==1){
$criteria->addCondition('editBevestigd=1');
}
//for column set or not
$criteria->addCondition('edit IS NOT NULL AND editBevestigd=1');

Update field in table for all records using a select statement

A previous developer created a table that stores the absolute path to files in our server. I want to convert them to relative paths instead.
I already wrote the portion that properly strips the string down to a relative path. My issue is understanding how to basically update each record, with a new version of its own string.
Here is what I originally tried:
UPDATE LFRX_Attachments
SET [File] = (SELECT TOP 1 SUBSTRING([File], PATINDEX('%Files\%', [File]) + 6, LEN([File]))
FROM LFRX_Attachments A
WHERE [Type] = 4 AND AttachmentId = A.AttachmentId)
However, this tanked in epic fashion by just overwriting every record to have the value of the first record in the table. Any suggestions?
UPDATE LFRX_Attachments
SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))
WHERE [Type] = 4
From a readability/maintenance standpoint, you're better off selecting for the data you want to alter, then iterating through the result set and updating each record separately.
Does this work for you?
UPDATE LFRX_Attachments SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))

sql to set an xml value

I'm a novice in mySql.
I'm trying to replace a value in the xml column of my table.
my select method works.
SELECT * FROM `comics` WHERE ExtractValue(xml,'comic/pageNumber') = 6
my replace method doesn't. I've been searching for the correct syntax for a bit now...
SET xml.modify(
replace value of ('comic/pageNumber') with 5
)
some background:
this situation comes up when i delete a comic page.
it leaves a gap in the page numbers, after which i would either:
iterate through all the comics and remove any gaps in the page numbers.
or
iterate through all comics with pageNumber larger than the deleted page, and reduce their pageNumber by 1.
How about
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>5</pageNumber>')
WHERE ExtractValue(xml,'comic/pageNumber') = 6
Tested on MySQL version 5.1
UPDATE `comics`
SET xml = UpdateXML(xml,
'comic/pageNumber',
concat('<pageNumber>',(ExtractValue(xml,'comic/pageNumber')+1),'</pageNumber>'))
WHERE ExtractValue(xml,'comic/pageNumber') >= 1
You'd be better off actually storing the fields in the table, rather than a single field with xml in it. Then the following would work. Otherwise there's not much point using a relational database at all.
BEGIN;
DELETE FROM `comics`
WHERE `comicID` = :id AND `pageNumber` = :page;
UPDATE `comics` SET `pageNumber` = `pageNumber` - 1
WHERE `comicID` = :id AND `pageNumber` > :page;
COMMIT;

Rails botches the SQL on a complex save

I am doing something seemingly pretty easy, but Rails is messing up the SQL. I could just execute my own SQL, but the framework should be able to handle this.
Here is the save I am trying to perform:
w = WhipSenVote.find(:first, :conditions => ["whip_bill_id = ? AND whip_sen_id = ?", bill_id, k])
w.votes_no = w.votes_no - 1
w.save
My generated SQL looks like this:
SELECT *
FROM "whip_sen_votes"
WHERE (whip_bill_id = E'1' AND whip_sen_id = 7)
LIMIT 1
And then:
UPDATE "whip_sen_votes"
SET "votes_yes" = 14, "updated_at" = '2009-11-13 19:55:54.807000'
WHERE "id" = 15
The first select statement is correct, but as you can see, the Update SQL statement is pretty wrong, though the votes_yes value is correct.
Any ideas? Thanks!
It would help to see the WhipSenVote model.
you can also use decrement!