can we use two update queries for one table - php

Is it possible to update one table with 2 queries.
$imp_feature=$_POST["imp_feature"];
$project_data=array(
"feat"=> $imp_feature
);
$db->where("post_project_id",$proj_id);
$project_id=$db->update("post_project",$project_data);
$del_feature=$_POST["del_feature"];
$projects_array=array(
"feat"=> $del_feature
);
$db->where("post_project_id",$proj_id);
$project_result=$db->update("post_project",$projects_array);
I tried but failed.if it is possible then tell me how

Related

batch delete in codeigniter

insert_batch() is a codeigniter build-in function which inserts 100 data of rows at a time. That's why it is so much faster for inserting large amount of data.
Now I want to delete large number of data like insert_batch() function does.
Is their any way to do it?
Already I am using where_in function but it is not that much faster like insert_batch() and that's why timeout error occur often.
I want to know specially that can i make a function like insert_batch() or insert_update() in codeigniter system/database/db_query_builder ?
If I can how to do it. or any other suggestion please ?
If we are talking about alot of rows, it may be easier to perform a table 'switch' by inserting and dropping the the original table.
However one drawback to this will mean any Auto Increment IDs will be lost.
<?php
// Create a Copy of the Same Data structure
$this->db->query( "CREATE TABLE IF NOT EXISTS `TB_TMP` .. " );
// Select the data you wish to -Keep- by excluding the rows by some condition.
// E.g. Something that is no longer active.
$recordsToKeep =
$this->db
->select( "*" )
->get_where( "TB_LIVETABLE", [ "ACTIVE" => 1 ])
->result();
// Insert the Excluded Rows.
$this->db->insert_batch( "TB_TMP", $recordsToKeep );
// Perform the Switch
$this->db->query( "RENAME TABLE TB_LIVETABLE TO TB_OLDTABLE" );
$this->db->query( "RENAME TABLE TB_TMP TO TB_LIVETABLE " );
$this->db->query( "DROP TABLE TB_OLDTABLE" );

Laravel from database table to another table 1mill rows, slow

So basically, I got 2 tables.
The first table contains 1 million rows or something, with an empty field called 'telefon'.
Now, I got a second table, which has the field values for 'telefon' in the other table.
I came up with this solution, but this takes forever. It has been an hour, and when inspecting the database table, only 1600 rows are done. Is there any faster ways of doing this? Thanks in advance.
DB::table('phones') -> orderBy('id') -> chunk(100, function($old) {
foreach ($old as $x) {
DB::table('companies')
-> where('organisasjonsnummer', $x -> businessCode)
-> update([
'telefon' => $x -> contact
]);
}
});
Huh, foreach + queries is almost always bad. If I am not mistaken, you would like to do this:
UPDATE companies, phones SET companies.telefon = phones.contact WHERE companies.organisasjonsnummer = phones.businessCode
It may be very slow if there's no index on companies.organisasjonsnummer and phones.businessCode columns, but it can take a lot of time to index them now as well, so I'm not sure if there's any benefit to index them now if they won't be used later. Anyway, using a single query should be faster at least to some extent.
Always remember, when you use Eloquent/SQL inside a loop, you will run a command for each round.
The same applies for lazy loading.
In this case you should use \DB::statement("put your sql here"); or in this case \DB::update("put your update here");, let the database do the service for you and be happy!.

How to update a column in the database using SQL

I am having two tables, in one of the table I am inserting the data and in the other table I want to update a single column. But if I use update, it is updating all the columns in that table. It is inserting correctly. Can any one help me?
$this->db->trans_begin();
$data=array(
'company_name'=>$this->input->post('company_name'),
'category_id'=>$this->input->post('category_id'),
'quantity_rec'=>$this->input->post('quantity_rec'),
);
$this->db->insert('inventory',$data);
$a=$this->input->post('quantity_rec');
$b=$this->input->post('stock');
$c=$a+$b;
$data1=array(
'stock'=>$c,
);
$this->db->where(array('department_category.category_id'=>$id));
$this->db->update('department_category',$data1);
$this->db->trans_complete();
}
$this->db->update('department_category',$data1);
should have parameter where
$this->db->where(somewhere);
if no well, u change entire data dude

Joining Customer on Attribute

I'm trying to filter my orders which are returned back by the magento API by a customer attribute. I tried several approaches but nothing seem to work.
I'm using Magento 1.4.1.1 atm and the api does this at the moment:
$billingAliasName = 'billing_o_a';
$shippingAliasName = 'shipping_o_a';
$collection = Mage::getModel("sales/order")->getCollection()
->addAttributeToSelect('*')
->addAddressFields()
->addExpressionFieldToSelect(
'billing_firstname', "{{billing_firstname}}", array('billing_firstname'=>"$billingAliasName.firstname")
)
->addExpressionFieldToSelect(
'billing_lastname', "{{billing_lastname}}", array('billing_lastname'=>"$billingAliasName.lastname")
)
->addExpressionFieldToSelect(
'shipping_firstname', "{{shipping_firstname}}", array('shipping_firstname'=>"$shippingAliasName.firstname")
)
->addExpressionFieldToSelect(
'shipping_lastname', "{{shipping_lastname}}", array('shipping_lastname'=>"$shippingAliasName.lastname")
)
->addExpressionFieldToSelect(
'billing_name',
"CONCAT({{billing_firstname}}, ' ', {{billing_lastname}})",
array('billing_firstname'=>"$billingAliasName.firstname", 'billing_lastname'=>"$billingAliasName.lastname")
)
->addExpressionFieldToSelect(
'shipping_name',
'CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})',
array('shipping_firstname'=>"$shippingAliasName.firstname", 'shipping_lastname'=>"$shippingAliasName.lastname")
);
Which is the default API call I guess. Now I just want to join a customer attribute called update - how do I achieve this simple task?
Or is this not possible on a flat table like sales_flat_order?
Whenever I need to do this I use something like:
Joining An EAV Table (With Attributes) To A Flat Table
It's not well optimised but you should be able to pick out the parts you need.
PS.
I think I'll explain what I mean by optimised since it's important. In the heart of the method is this bit:
->joinLeft(array($alias => $table),
'main_table.'.$mainTableForeignKey.' = '.$alias.'.entity_id and '.$alias.'.attribute_id = '.$attribute->getAttributeId(),
array($attribute->getAttributeCode() => $field)
);
If you know MySQL then you'll know it will only pick one index when joining a table, the more specific the better. In this case only the entity_id and attribute_id fields are being used so MySQL is restricted to those. Both columns are indexed but the cardinality is low.
If the condition also included the entity type then MySQL would have the choice of using IDX_BASE which indexes the columns entity_type_id,entity_id,attribute_id,store_id in that order (it needs to process them left to right). So something like this results in a much improved EAV performance - depending on how many rows on the 'left' table it could be several hundred- or thousand-fold better.
$alias.'.entity_type_id='.$entityType->getId().' AND main_table.'.$mainTableForeignKey.' = '.$alias.'.entity_id AND '.$alias.'.attribute_id = '.$attribute->getAttributeId().' AND '.$alias.'.store_id=0'

Multiple(sharding ) table in a Model CakePHP

There are many tables as the following:
table_2010
table_2009
table_2008
table_2007
.
.
Using MySQL 4 + PHP5 + CakePHP 1.3
My Question is
How to treat these tables in a model?
I wanna treat like this
Table->find('all',"2010",array("conditions"=>""));
I agree with Nik -- unless you're sharding for performance reasons, I would combine all of your tables into one table, with a column for the year (if you make it an INT, it won't affect performance much).
However, if you need to shard your tables, I'd recommend that you just override the Model::find() method to accept additional parameters. In your model, write something like the pseudocode below:
function find( $type, $options = array() ) {
if( isset( $options['table'] ) ) { // this is the index where you'll pass your table name
$this->setSource( $options['table'];
}
return parent::find( $type, $options );
}
Basically the call to setSource will change your table that you are querying, at runtime. See Can a CakePHP model change its table without being re-instantiated? for more information.
For me the smarter way is to use one table - posts for example and in that table to have a special column called year. So the find will be something like:
$this->Post->find('all', array('year'=>2010));

Categories