is have this statement and i want to make it by the Active Records way in codeigniter
DELETE FROM TABLE
(col1 = value AND col2 = value2 ) OR (col1 = value2 AND col2 = value );
CodeIgniter Active Record is impractical for mixing AND's and OR's within a query.
You will likely need to construct some of the query manually, e.g. something along the lines of:
$this->db->where("col1 = $value AND col2 = $value2");
$this->db->or_where("col1 = $value2 AND col2 = $value");
Alternately, in your example where there are only two specific columns and two values, the following idea should also work:
$this->db->where_in('col1', array($value, $value2));
$this->db->where_in('col2', array($value, $value2));
$this->db->where('col1 != col2');
Try this:
$this->db->where('col1', $value1);
$this->db->where('col2', $value2);
$this->db->delete($table);
That's not the goal of active record. As name suggest active record object instance is tied to a single row in a table. If you want to do this in "active record way" you have to retrieve all the records you have to delete the database, mark them as deleted and commit changes witch is overkill. Best you can do is to get connection instance from codeignither and execute query manually.
Related
Address Table with 50 records. Has a bool field called "primary_address_indicator".
I want to update a selected record to true and automatically set all other 49 records to false.
I know I can do this with 2 sql statements
First (update all records to false)
UPDATE address SET address.primary_address_indicator=0
Then (update specific record to true)
UPDATE address SET address.primary_address_indicator=1 WHERE address.record_id=31
Is there a way to do this in a single sql statement? Something like #Rank?
Keeping it simple... no Unions or some weird self table join.
Maybe what I am looking for does not exist...and that is fine too. just curious.
Update with Case
UPDATE tableName
SET Pin = CASE
WHEN 1=1 --'your condition'
THEN True
ELSE False
END
PC : #keWalker
The simplest way is simply to treat the boolean result as a number:
UPDATE address a
SET a.primary_address_indicator = (a.record_id = 31);
CASE is the formal way to do it. This short-hand is specific to MySQL but it is one of the database-specific features that I like.
What I am trying to do is reduce the time needed to aggregate data by producing a roll-up table of sorts. When I insert a record, an after insert trigger is fired which will update the correct row. I would update all of the columns of the roll-up table if I need to, but since there are 25 columns in the table and each insert will only update 2 of them, I would rather be able to dynamically select the columns to update. My current update statement in the after insert trigger looks similar to this:
update peek_at_chu.organization_data_state_log odsl
inner join ( select
lookUpID as org_data_lookup,
i.interval_id,
peek_at_chu.Get_Time_Durration_In_Interval1('s', new.start_time, new.end_time, i.start_time, i.end_time) as time_in_int,
new.phone_state_id
from
(peek_at_chu.interval_info i
join peek_at_chu.interval_step int_s on i.interval_step_id = int_s.interval_step_id)) as usl on odsl.org_date_lookup_id = usl.org_data_lookup
and odsl.interval_id = usl.interval_id
set
total_seconds = total_seconds + usl.time_in_int,
case new.phone_state_id
when 2 then
available_seconds = available_seconds + time_in_int
end;
In this, lookUpID is a variable previously declared in the trigger. The field that will dictate which field of the roll-up table to update is new.phone_state_id. The phone_state_id's are not consistent, that is some numbers are skipped in this table, so an update based on column number is out the window unless I create a mapping.
The case option throws an error but I am hoping to use something similar to that instead of 25 if statements if I can.
You have to update all the columns, but use a conditional to determine whether to give it a new value or keep the old value:
set total_seconds = total_seconds + usl.time_in_int,
available_seconds = IF(new.phone_state_id = 2, available_seconds + time_in_int, available_seconds)
Repeat the pattern in the last line for all the other columns that need to be updated conditionally.
I have a select statement:
SELECT id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Here's the output:
id content name
99708 10.6.252.41 server01.example.org
What I'd like to do is be able to get the id that is returned from the previous statement and USE the id as input into another statement (an UPDATE statement) that will increment the value of a single column in the same table.
An example UPDATE statement that I am wanting is:
update records SET hits = hits + 1 WHERE id = ID_FROM_SELECT;
Thanks in advance.
You can use user defined session variables for this if the SELECT is returning just one result:
SELECT #id:=id AS id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Then, on the same database session (connection), do the following:
UPDATE records
SET hits = hits + 1
WHERE id = #id;
I'm assuming you're doing something with the selected records in your app, and you're trying to save on performance by avoiding having to search for the record again in the UPDATE. Though, in that case, why not set the 'id' value as a parameter in code?
Obviously, if the SELECT is returning multiple records, this would best be done in code as I mentioned above, otherwise you're left with running the SELECT query again as a subquery:
UPDATE records
SET hits = hits + 1
WHERE id IN
(SELECT id
FROM records
WHERE type = '1'
AND name = 'test');
So, then, it makes more sense just to apply the same filter to the UPDATE instead:
UPDATE records
SET hits = hits + 1
WHERE type = '1'
AND name = 'test'
Probably this is not what you want to do.
First of all...If the query only returns 1 line, the solution provided by Marcus Adams works fine. But, if the query only returns one line, you dont need to preset the id in order to update. Just update it:
update records
set hits = hits + 1
where type = '1'
and name = 'test'
Second...If the query will not return only one record and you want to update all records returned with same values or calculations, the same code above will do what you need.
Third, if the query does not return just one record and you need to update each record returned with different value then you need to have a different approach.
I think you are not designing your system very well. If the request for update come from outside, you should have the id to be updated as a parameter of your request. For example something like:
<html>
<body>
Test
</body>
</html>
And in your update.php you have something like:
<?php
$id = $_GET['id'];
$sql = "update records set hits = hits + 1 where type = '1' and name = 'test' and id = $id";
?>
Of course, the picture I have is to small. Probably you have a reason to do this way or this is just an example. If you fill us up with more info we might be more helpful.
SQL:
$mysqli->query("UPDATE results
SET result_value = '".$row[0]['logo_value']."'
WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'
AND user_id = '".$user_data[0]['user_id']."'");
This results table also contains result_tries I'd like to fetch before doing update, so I can use it to modify result_value... Is there a way to do it in a single shot instead of first doing select and than doing update?
Is this possible?
Basically:
UPDATE results SET result_value = result_value + $row[0][logo_value]
for just a simple addition. You CAN use existing fields in the record being updated as part of the update, so if you don't want just addition, there's not too many limits on what logic you can use instead of just x = x + y.
If I were to translate my MySQL table into PHP it might look something like this:
$table = array();
$table[0] = array ('id'=>0, 'f1'=>0, 'f2'=>1);
$table[1] = array ('id'=>1, 'f1'=>0, 'f2'=>2);
// etc...
In that case, I would want to do something like this:
foreach($table as $row) {
$row['f1'] = $row['f2'];
}
Is it possible to do this with a single MySQL statement utilizing select and update?
I was imagining something like this:
update myTable set f1=(select f2 from myTable where id=id);
Except I don't think that would work... I'm not sure how to say where id in the second statement is equal to the id in the first statement. Or how to apply it to all rows in the table.
How could I do this?
Actually, to update the f1 values to be the same as the f2 ones you'd use:
UPDATE mytable
SET f1 = f2
If you don't specify a WHERE clause, the query will apply to all rows.
Just
UPDATE myTable SET f1 = f2
the expression on the right of the = is in terms of existing columns of the same row (one often does, e.g, SET col = col + 1... it doesn't have to be OTHER columns, though it can). The lack of a WHERE clause means the update will happen on all rows, as you seem to want.