Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10
Related
I have a table as follows:
I want to select the first row having the unit value greater or equal to 2000 and less than 3000. I can do it as follows:
select * from spot_shipment_fees where min_unit>=2000 AND max_unit<3000
But the problem is I have only one variable holding a single value like 2000. How can I generate this SQL in this situation?
i.e: $unit_value = 2000.
select * from spot_shipment_fees where min_unit>=$unit_value AND max_unit<$unit_value
I know this is wrong but can't find the tricks to resolve it. Any idea?
I think you just want:
select ssf.*
from spot_shipment_fees ssf
where ? between min_unit and max_unit;
? is a parameter placeholder for the value you want to pass in.
I've got a database with question numbers between 1 and 21 all with answers / values etc.
But I want to be able to delete for example question 7.
And if I delete that question that is between other numbers that the question numbers will automatically change from 1-21 to 1-20 instead of 1-6, 8-21.
Is this possible? I tried searching on the web but I couldn't find what I was looking for.
With kind regards,
How do I make this shorter? I know it can be done with innerjoin but I dont know how to correctly use it because the explanations are unclear to me..
`$sql = "DELETE FROM insertquestion
WHERE nummer='".$questionNumber."'";
$deleteFromQuestion = $db->prepare($sql);
$deleteFromQuestion->execute();
$updateSql = "UPDATE insertquestion SET nummer = nummer - 1 WHERE nummer >= '".$questionNumber."'";
$updateSql = $db->prepare($updateSql);
$updateSql->execute();`
and that another time but then with another table (vraag)
You can use Triggers to do this operation ref:
http://www.mysqltutorial.org/mysql-triggers.aspx
or use a update query to set the values (however this method is costly, if tthe number of rows is great in number)
Update table set q_no=q_no-1 where q_no> 5 //(if you delete a question with number 5)
There may be many ways for this requirement. One of them is you can achieve it by trigger. Write an after delete trigger which will update all question numbers of the table.
CREATE TRIGGER tblquestion_delete AFTER DELETE on tblquestion
FOR EACH ROW
BEGIN
UPDATE tblquestion
SET qno = qno - 1
WHERE qno > old.qno;
END
Another way is simply you can write this query immediately after delete query.
$sql = "DELETE FROM insertquestion WHERE number='".$questionNumber."' ;
UPDATE tblquestion SET number = number - 1 WHERE number > '".$questionNumber."' ; ";
I need to update a the employee hours column with the current numeric value that is already existed in the database.
$mysqli->query("UPDATE `".MYDATABASE."`.`".MYTABLE."`
SET employeeHours='".$employeeHours."'
WHERE sessionID='".session_id()."'");
This like is working properly but needless to say that it's overwriting the old value.
My goal is to add the current $employeeHours to the existed value in the database.
I assume that it's done with concat but I'm not sure about the syntax.
Small tweak needed here...
I couldn't believe that it would be so ridiculously simple :
$mysqli->query("UPDATE `".MYDATABASE."`.`".MYTABLE."` SET employeeHours = employeeHours + '".$employeeHours."' WHERE sessionID='".session_id()."'");
Thank you #castis!
I have a question about inserting row order number by spesific order type.
Products table has OrderNumber field. I want to programaticly add new line to appropriate OrderNumber by its name. If the reference column would be integer it has to be easy like that
update products set OrderNumber=OrderNumber+1 where Price>555
Is there similar way for varchar field like
update products set OrderNumber=OrderNumber+1 where Name>'bla%'
Thank you
You can use STRCMP('text', 'text2')
update products
set OrderNumber=OrderNumber+1
where STRCMP(Name, 'bla') = 1;
I missunderstood your point. Can you try something like this?
SET #rownum:=0;
update
set OrderNumber=#rownum:=#rownum + 1
from products
order by Name;
You can simply run an update query with a sequential number like in here
Sounds like a bad design. Why not simply have a plain-jane auto_increment field and order by that? Every new record would by defnition have a higher ID than any of its predecessors.
you mean something like update products set OrderNumber=OrderNumber+1 where Name like 'bla%'
I wanted to know whether i can direct update mysql datas to add (+) the values, without needing to fetch them.
For example:
My database is like this,
data1 = 56
so to add a 4 to it, i will first fetch the data from the database then,
$data1 = $data1 + 4;
and finally again perform a Update query.
So is there is way i can send like add +4 to the current value of data1.
I am using php lanuage.
Thank You
Like... this?
UPDATE
MyTable
SET
Data = Data + 4
WHERE
Myid = 123
A look into basic SQL syntax seems advisable. ;-)
If you are working with PHP to manipulate your data, a deeper look into the mysqli_* functions family in general and prepared/parameterized statements in particular is advisable as well.
mysql_query("UPDATE `mytable` SET `data1` = `data1` + 4");
You'll want a WHERE clause unless you want to update every row.