Trying to update one table from a column in another table based on two conditions. I've tried many version of this but this is what I'm currently trying and it just hangs. When I check the table while running, I'm not seeing any updating occurring. Is there something wrong with my syntax? Thanks!
UPDATE earningsdates e
INNER JOIN actualmove a
ON e.ticker = a.theticker AND e.exactearningsdate = a.realearningsdate
SET e.theamove = a.themove;
I made ticker and theticker non-unique indexes and the query worked without a problem.
Related
I am using code-igniter version 3.1.6 and xampp control panel v3.2.2 ,listing customer details by table wise and join two table 'tbl_customer' and 'tbl_additional_details' tbl_customer"cus_id" is set a index and primary key
My Query like
$this->db->select('*');
$this->db->from('tbl_additional_details');
$this->db->join('tbl_customer','tbl_additional_details.customer_id = tbl_customer.cus_id ');
$this->db->where('tbl_additional_details.branch_id',$_SESSION['branchs']);
$this->db->order_by('tbl_additional_details.customer_id',"desc");
$query = $this->db->get();
return $query->result();
why the listing show very slowly any way to improve the result time?
Queries optimisation is a complex topic... you could do with running the SQL in MySQL directly and doing some profiling there, but start with:
Make sure you create an index on any field that you might select or join on.
Run "optimize" on all your tables... you can do that from phpmyadmin. Do this AFTER creating your indexes.
Consider "denormalization", i.e. merge the tables or duplicate certain fields. For example if you are joining 2 tables for the sake of getting ONE extra field, you might want to copy that extra field over to the first table and eliminate the join altogether. This produces drastic increases in speed on large tables. Look at example here: http://courses.ischool.berkeley.edu/i257/f99/Lecture9_257/sld018.htm
All the best!
I have the following query:
update tblwerknemerdienst toUpdate
set datumtot = (select subdate(datumvanaf,1)
from (select * from tblwerknemerdienst) nextDienst
where nextDienst.Werknemer = toUpdate.Werknemer
and nextDienst.datumvanaf > toUpdate.DatumVanaf
order by DatumVanaf
LIMIT 1)
WHERE DatumTot IS NULL;
The query runs fine on MySql versions other than MySql 5.7.10. I've searched around the web and found that you have to set derived_merge=off, but sadly this had no effect and the query still fails with the same error.
I have also tried several different ways of rewriting the query, but all to no avail.
Is there something I'm missing or is there another way to accomplish this?
In the end I fixed this by rewriting the whole thing in a procedure, where I used a cursor to execute the query and get the necessary data. Then I perform the update statement based upon the fields selected in the cursor.
This seemed to be the only way to reliably perform the operation required on different versions of MySql.
I'm trying to update two tables in MySQL with one query, and am running into an error. I've looked at similar situations and resolutions, but can't seem to translate them to my specific query/situation as I am continuing to get the same error. My goal seems like there is an obvious solution I am missing. My Query is as follows:
UPDATE table_pr, table_pu
INNER JOIN table_pu ON table_pr.id = table_pu.pr_id
SET table_pr.cpr2 = table_pu.cpr2_id, table_pu.cpr_updated = '1'
WHERE table_pu.cpr2_date < CURRENT_DATE()
When I run the Update, I get Not unique table/alias: 'table_pu' returned. I prefer to not use aliases in this, but I've also tried to set unique aliases for the tables with the same result. If my approach should be modified, my ultimate goal is to have table_pr.cpr2 set to table_pu.cpr2_id based on WHERE table_pu.cpr2_date < CURRENT_DATE() and if the update is run, to also set table_pu.cpr_updated = '1'
Any help or guidance would be greatly appreciated.
Removing the second table reference in the UPDATE statement resolved the issue.
Is this the right syntax:
INSERT INTO stock (Image)
SELECT Image,
FROM productimages
WHERE stock.Name_of_item = productimages.number;
SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes and such. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.
I use this all the time and it works fairly well.
INSERT INTO bar..tblFoobar( *fieldlist* )
SELECT *fieldlist* FROM foo..tblFoobar
This just moves the data. If you want to move the table definition (and other attributes such as permissions and indexes), you'll have to do something else.
The query logic that you are trying appears to be not correct (the query itself is buggy).
Assuming you have the correct query for the above logic and what you are trying is to insert new rows into the table stock by selecting a column from productimages table with a matching record as stock.Name_of_item = productimages.number
The above logic will add redundant data in to the table.
You perhaps looking to update instead of insert, something as -
update stock s
join productimages p on p.number = s.Name_of_item
set s.Image = p.Image
I am trying to execute an UPDATE statement as follows:
UPDATE table_name
SET goal_seq = 'xyz'
WHERE league_id = 20
AND home = 0
AND away = 0;
In trying to work out why it wasnt working I have established that you cant have multiple WHERE clauses (is that correct?) but I cant figure out how to get round it.
Any help much appreciated,
P
you CAN have multiply conditions after WHERE statement - go to Mysql docs for more information and some examples - i believe the key table in your code is just a replacement for actual table name? If this query doesn't work pls provide the error which server returns - without it we cannot help you.