Update whole row - mysql

Does MySQL have an UPDATE syntax that would allow me to update a whole row (not individual columns)?, something like this:
UPDATE `table1`
LEFT JOIN `table2` USING (`myColumn`)
SET `table1`.* = `table2`.*
Both table1 and table2 have the exact same structure.
The column names are variable, and subject to change.
Unfortunately I can't use a REPLACE query because the tables were badly designed and I'm not at liberty to modify them. The joining column doesn't have a unique index assigned to it, it only has a normal index.

"The column names are variable, and subject to change" - This is so wrong, if you rely on column order not col names, one day it will hit you in the head. Update will work with explicit col names: sqlfiddle.

Related

how to rename id column in query MYSQL

I have this column named "id". whenever I try to inner join like this "inner join tblBarangay as LB USING(id) I get an error Unknown column 'id' but when I try to rename it to 'brgy_id' it works (Is the word id in MYSQL is a function name?). I don't want to rename it because many of my PHP code queries will then needed to be changed. Now, how can I make it work without renaming the column name? Thanks.
TableA:
-------------------------------------------
id | brgy_name | description
1 New York a city
TableB:
I don't use using because first off I am too dense. Second, it is too implicit and I like to be explicit so I understand it and others too. So, if you just don't use using (because they don't have id in common as column names) you are all set by explicitly joining with table1.id = table2.whatever. And you don't have to alter your table at all.
The Takeaway: Don't use using for this situation. And don't confuse USING with some mandate that you need to alter your table forever, thus messing up all you other queries that work just peachie.
From the MySQL Manual Page entitled JOIN Syntax:
What is the schema of second table, may be you dont have Id in second table "TableB"

why we provide alias names after 'DELETE' in multiple delete

This might be a very stupid questions, but I am keen to know if anyone has any suggestions:
Single Table Delete Correct Query : delete from vehicle_owner where id=3;
Single Table Delete InCorrect Query : delete from vehicle_owner v where v.id=3
Mulitple Table Delete : delete v,s from vehicle v , category s where v.id=3 and v.id=s.id;
Qs 1: I was wondering that why 1st is right and why 2nd is incorrect. Basically I am looking for logical answers which explains why providing an alias in delete query is incorrect.
Qs 2: why we keep two alias names after 'DELETE' keyword for multiple delete. Anyways we are providing complete details in join condition. So why it is designed in such a way.
The second is incorrect because if you use an alias, the SQL statement is considered a version of the multi-table DELETE that just happens to have only one table specified. As such, it must follow the other rules of multi-table DELETE. There's no room in the syntax for single-table DELETE to specify an alias.
You don't need to have aliases, but you do need to specify which of the tables you are deleting from. So you list the tables, or their aliases, directly after the DELETE keyword. Otherwise, MySQL won't know whether to delete rows only from the first table (with the other tables being present to filter) or to delete from all tables, or some combination.
See: https://dev.mysql.com/doc/refman/5.5/en/delete.html
The full correct syntax for your second example would be:
delete v from vehicle_owner as v where v.id = 3
Once you've specified the alias, you need to also specify it in the delete clause.
For your second question, I'm not quite sure what you're asking.

Update a column based on two matching values of a column in two tables

I have two questions which if someone can help me it would be great and would be a great learning for me.
I have two tables and my requirement is to update a column A of
Table 1 value only for those rows for which the column B has
values same as column B of Table2.
I am looking for an optimized query for this in SQL.
UPDATE DBA.COM, DBA.MEN
SET DBA.COM.ND_MAN=''
WHERE DBA.MEN
After this, I couldn't select column names in where condition.
The problem I am finding in Column B of both the tables is, it is
unique identified (GUID) from the UI. So, when I copy the cell value
from "SQL Anywhere" Interactive SQL Editor, it displays the column
value copied as follows:
0x99e2f2a23f9946acb0ceb374a627b142
and not as 99e2f2a23f9946acb0ceb374a627b142.
However, both the table's column value when I copy, it is starting
with 0x. So will it not pose any problem I guess?
Or how to rectify it in above query which you will create for question 1?
You need to join and update something as
update table1 t1
join table2 t2 on t1.B = t2.B
set t1.A = 'some value'
Answer to your first question
UPDATE t1, t2 SET t1.name = new_value WHERE t1.id = t2.id;
Notes:
A multiple-table UPDATE is an extension of a single-table statement:
Following the UPDATE keyword, name the tables involved in the operation, separated by
commas. (You must name all the tables used in the query, even if you aren’t updating all
of them.)
In the WHERE clause, describe the conditions that determine how to match records in the
tables.
In the SET clause, assign values to the columns to be updated. These assignments can
refer to columns from any of the joined tables.

INSERT ... SELECT to same table

I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.

how to change a lot of records at once in phpmyadmin

I'd like to know how to update several records at once where a certain column type is selected.
I have found how to select records. I have found how to update records. But i don't know how to do it both together for it to work.
Selecting:
SELECT * FROM users WHERE 'type'='new'
Updating:
update table
set column = 937
So basically i want to change the info in the 'column' to 937 in the 'table' if another column 'type' is 'new'.
Thanks,
You can do this by simply adding a WHERE clause to your UPDATE statement:
UPDATE `users`
SET `myColumn` = 937
WHERE `type` = 'new'
Of course, change myColumn to match your column name
You can do this with a subquery :
update users
set column = 937
where id in (select id from users where type='new')
Just change the columns name if I got them wrong.
After researching for a while I found another solution to this problem. When referencing the same table or field name in a query, the name space in MySQL ends up with duplicates and fails. To overcome this use the "AS" syntax to name the duplicate items. Also, update queries often require a key field to be used as the parameter for the where clause, this is often your auto-incremented ID field. This example provides a solution for both of these problems.
UPDATE tbl
SET field=newValue
WHERE tbl.key IN
(SELECT idNew FROM
(Select tbl.key AS idNew
FROM tbl
WHERE field=editValue) AS tblNew);
tbl - Table name being updated
field - Target field for update
newValue - Value to replace items in the target field
tbl.key - Field name for the key in your table
idNew - New name for key in your table, to replace the name and prevent failure from duplicating names in query. could be any alphanumeric string. 'id' or 'id_new' or 'id_n'
editValue - The value to change
tblNew - New name for query, to prevent duplicate table names in the query. Could be any alphanumeric string 'tbl_n' or 'tbl_new' or 'tbl_test'
This query gets the key values for all the records that match records that have values you want edited, then changes the names of the key and tbl so they can be processed by MySQL, lastly the update query runs and changes the values based on the keys provided in the sub-query.
Hopefully this saves someone else a few hours!