I have this query update that needs 2 sequence to complete the job. It's like this :
Model:
$result = DB::statement( DB::raw("SET #a:=0;") );
$numbering = DB::update(DB::raw(UPDATE table SET number:= #a+1, table_number = number+1 ));
return $numbering;
if i executed that function, only the number column is filled with value #a+1 , but the table_number column is still blank. I have to run the function twice in controller to get table_number column filled with the values number+1
The number column isn't yet set at the time of the first update so use
UPDATE table SET number:= #a+1, table_number = #a+2
Related
I want to update the same value in DB,
But the database returns 0 when I do that.
is it possible to return 1 or true? or force the DB update?
$sth =$pdo->prepeare("UPDATE table set x = :x where id=:id")
$sth->execute()
$result = $sth->rowCount() // that return 0;
x is the same value in DB and the result is 0 but i want to let the user to approve the form even with the same value
Updating with same values will always return false. You need to add some some distinct value that could be updated .
For example you can have field that stores timestamp (like updatedOn) so every time you pass same values for rest of field, it will be accepted as time stamp field will have unique value.
basically you will need to add a WHERE clause as you said you need to update some values
$sth =$pdo->prepeare("UPDATE table set x = :x where y = :y")
I have a localhost and I want to do something I hope I can explain it.
I have a table named 'students' in a database named 'theway', there is a column named 's_class_id' its kind is auto increment , and in php I insert some values in this table. But I want to insert a variable+the auto increment in the column named 's_class_id'.
For example : The variable's value is 'a', and the auto increment value is '5'. I want the value which be inserted in the 's_class_id' like this 'a5'.
Any help?
If it is auto-increment, the field should be an integer. Do not think that it is possible to change it to eg: 'a5'. You could run an update query searching for the s_class_id and update another string field to set it to be 'a5'.
You can do that task by folowing method,
First you need to insert record into theway table. After record insertion mysqli_insert_id($con) will return last inserted id. Then you need to update that row by using last inserted id.
// Insert Record
mysqli_query($con,"INSERT INTO theway (s_class_id) VALUES ('a')");
// Getting last inserted id
$lastInsertID = mysqli_insert_id($con);
$updatedValue = 'a'.$lastInsertID;
// Update same row with last inserted ida
$SameRowUpdate = "UPDATE theway SET s_class_id='".$updatedValue."' WHERE id=".$lastInsertID;
if (mysqli_query($conn, $SameRowUpdate )) {
echo "Record addedd successfully";
}
I have a table which has columns status and new_status, so when i change status i put in new_status the new value for status.
But i want to have a backup solution, so i need an update which set value for new_status value from column status.
I use codeigniter, i tried
$data = array(
'new_status'=>Status,//'new_status'=>'Status'//'new_status'=>`Status`
);
But none above worked.
I made a custom query and i called with
$query = $this->db->query($text);
and $text = "UPDATE table SET new_status = status WHERE [condition]" and it's working 100%.
I use this following code to print the count value from the db table now i need to insert this count value into the table please help me.
$this->db->like($data);
$this->db->from('student_table');
$result= $this->db->count_all_results();
echo "count:".$result;
This only display the count value
need to insert the count value to the db table
its very simple, insert count value in database, as u have not mentioned that table name and attributes/fields in which you want to insert the count, i am putting up a general answer!
using Insert query
$this->db->like($data);
$this->db->from('student_table');
$result= $this->db->count_all_results();
$this->db->query("INSERT INTO tbl_name VALUES('$result')");
or if you want to count all rows in a table,
$result = $this->db->count_all('table_name');
Using update query
$this->db->where('id', $id); // if particular value to update
$this->db->update('tblName', $result);
Currently trying to find a way to do the following inside some form of loop (preferably without a performance hit on database).
I have 3 tables user_hours, user_calendar and hours_statistics. I need to first do:
SELECT user_calendar.date_start,
user_calendar.opportunity_id,
user_hours.user_id,
user_hours.agreed_hours,
user_hours.completed_hours,
user_hours.hours_committed
FROM user_calendar
JOIN user_hours
ON user_calendar.user_calendar_id = user_hours.user_calendar_id
WHERE user_calendar.date_start = CURRENT_DATE()
AND user_hours.completed_hours IS NULL
AND user_hours.hours_committed = 'accepted'
This query could return like the following:
http://i.imgur.com/5cJ5v.png
So for each opportunity_id and user_id returned i'd like to then do:
UPDATE user_hours
SET completed_hours = agreed_hours,
hours_committed = 'completed'
WHERE opportunity_id = {opportunity_id}
AND user_id = {user_id}
AND hours_committed = 'accepted'
AND completed_hours IS NULL
Note that {opportunity_id} and {user_id} would need to be looped at this point (see screenshot) because we need to go through each user on each opportunity.
Then for each updated record i'd need to then get the total hours like:
// Get hours they have done to send to statistics data table
SELECT sum(completed_hours) FROM user_hours WHERE user_id = {user_id} AND opportunity_id = {opportunity_id}
// Get the completed hours total somehow as a variable
$completed_hours = (from result above)
// Commit stats
UPDATE hours_statistics SET completed_hours = (completed_hours+$completed_hours)
WHERE user_id = {user_id} AND opportunity_id = {opportunity_id}
Could anyone help write this as a procedure or a trigger of some kind or help me in the right direction to get a starting point for looping over this stuff? Manually the querying works, just need to be looped / automatic for a stats update to run.
You can create a trigger to update hours_statistics whenever user_hours is updated (you may also want to add similar triggers for INSERT and DELETE operations, depending on your application logic).
Assuming that a UNIQUE key has been defined on hours_statistics.(user_id, opportunity_id) one can use INSERT ... ON DUPLICATE KEY UPDATE within the trigger:
CREATE TRIGGER foo AFTER UPDATE ON user_hours FOR EACH ROW
INSERT INTO hours_statistics (user_id, opportunity_id, completed_hours) VALUES
(OLD.user_id, OLD.opportunity_id, -OLD.completed_hours),
(NEW.user_id, NEW.opportunity_id, +NEW.completed_hours)
ON DUPLICATE KEY UPDATE
completed_hours = completed_hours + VALUES(completed_hours);
Then you can use a single UPDATE statement (using the multiple-table syntax to join user_hours with user_calendar) to perform all of the updates on user_hours in one go, which will cause the above trigger to update hours_statistics as desired:
UPDATE user_hours JOIN user_calendar USING (user_calendar_id, opportunity_id)
SET user_hours.completed_hours = agreed_hours,
user_hours.hours_committed = 'completed'
WHERE user_hours.hours_committed = 'accepted'
AND user_hours.completed_hours IS NULL
AND user_calendar.date_start = CURRENT_DATE();