SQL: I need copy data from one field to another field in one table base on id of category - mysql

SQL: I need copy data from one field to another field in one table base on id of category
Like this (but this not work)
UPDATE z_jlogica_awards SET desc=desclong
FROM z_jlogica_awards WHERE z_jlogica_awards.parent='1';

Your query syntax is wrong. try this:
UPDATE z_jlogica_awards SET longdesc=desc WHERE z_jlogica_awards.parent='1'

Related

Update table in one schema based on another schema table column in same database

I have two schemas in the same database. I want to update a table in one schema based on the table in another schema where the column value matches.
The structure of my database is as follows.
structure of database
As an example, this is my table named "p" in 1st schema that is "public" and have column "id" and "name"
id name
3 Loral
1 Kim
2 Shawn
and this is the second table named "t" in the second schema that is "t_sc" and have column "id" and "name" but the names are different than table "p"
id name
1 kylie
3 deny
2 tom
Now I want to update table "p" names according to table "t" names where the id matches.
I have tried the following query
update p set p.Name = t_sc.t.Name WHERE p.ID = t_sc.t.ID
but got the following error
ERROR: missing FROM-clause entry for table "t"
I have tried multiple other ways too but I am not able to get the desired result. I am using Navicat for query and the database is Postgresql.
If this is really MySQL, you need to call your schema(s) specifically.
You can thank this user for the example below:
Update a column on a table from a table on another schema MySql
update schema1.table1
inner join schema2.table2
on schema1.table1.IDColumn = schema2.table2.IDColumn
set schema1.table1.column1 = schema2.table2.column2
I have found the answer myself. So I thought maybe I should post it for future visitors.
The query will be
UPDATE public.p a
SET name=b.name
FROM t_sc.t b
WHERE a.id=b.id
I was using the alias with column name p.Name of the Table which I want to update. Also I was using the schema name at the wrong time t_sc.t.Name It was causing the error.

How to write select query in mysql with dynamic table name and dynamic column name

Above is the table structure of database. For each ‘form_id’ in ‘ap_forms ‘ there is a dynamic table with dynamic column names like ap_form_{form_id}.
Columns in this table will created dynamically by element_id and option_id combination. (eg: for element_id=1 and option_id=2 then column name= element_1_2).
I want the query result from these tables and this will looks like
I tried with dynamic query and I think we can’t create dynamic query with dynamic table name and dynamic column name. Please help me to get the above query result.

Adding a column of data

Not been able to find something that matches what I am setting out to teach myself, so here goes:
I have added a column (called status) to an existing table (called fruit). All values in this new column are currently null. Other columns in this table are id (primary key int(11) ) and fruitname.
My question is this:
Is there a command I can use to populate this column in one go? Or do I need to update each row one by one?
Ideally I am looking for something that populates columns in the same way insert does to rows. Something where I can specify the table and column name and then list the values to fill down.
If you want the new column status to have same value for all records like Fresh then you can do it in one go like
update fruit set status = 'fresh'
Else, you have no other way than performing an UPDATE row by row and populate the status column value for each fruit record.

Pick Linked table named as an input table in the append MS query

I need to append the data to a Access Table ULAE. I wants parameters to be picked from another table LAE.This table has 4 columns(Group,le,qtr,Table name) that will be an input and some parameters will be part of where condition. However there is one column table name that will have linked tables. Is it possible to pick the table name (that would be a linked table in the same access file, from where the data would be appended to ULAE).Is it possible to implement this in MSquery?
What i need is to pick the table name from LAE and append the data into ULAE from the table name specified in the LAE.
my query is like
INSERT INTO ULAE( vdate, profile_id, le, ibu)
SELECT PERSIAN.vdate, PERSIAN.profile_id, PERSIAN.le, PERSIAN.ibu
FROM **PERSIAN**;
**Persion is the linked table name coming from LAE**
Thanks in advance.

Update multiple tables using a Stored Procedure

I want use a stored procedure to update multiple tables in a db. Each table has a GUID as the PK and there are FK's between the tables.
For example, one table is "Tool" with a column ID (the guid) and another table is "Type" with ID as guid again. There is a column in Tool called "TYPE_ID" that is a FK to the table Type with Type's Guid stored in it. I want to first update the Tool table and then after, update the Type table based on that FK.
UPDATE Tool
SET Name=#Name, [Enabled]=#Enabled, TestMode=#TestMode, SerialNumber=#SerialNumber,
Andon=#Andon, ChimeZone=#ChimeZone, Number=#ToolNumber
WHERE ID=#ID
Update Type
SET Type=#Type
WHERE Tool.ID=#ID AND
Tool.TYPE_ID=Type.ID
I know that this code is incorrect for the second update, but this is the gist of how I would like to be able to do it. Is there a way to not have to SELECT the FK Guid, store it, and use it the next update? If that is the only way, how do I do that?
Write your second update like this, joining the Tool table to the Type table:
UPDATE ty
SET Type = #Type
FROM Tool to
INNER JOIN Type ty
ON to.TYPE_ID = ty.ID
WHERE to.ID = #ID
Actually you can UPDATE FROM clause:
UPDATE Type
SET Type=#Type
FROM Tool INNER JOIN TYPE
ON Tool.TYPE_ID=Type.ID
WHERE Tool.ID=#ID;
See http://msdn.microsoft.com/en-us/library/ms177523.aspx for full UPDATE syntax.