I want to insert the id of one table into another in PostgreSQL. First I want to select the id of table1 where it equals a specific value and store that into a variable. In MySQL I can do:
SET #last_id_in = (SELECT id FROM table1 WHERE col1 = 1);
Then I can insert where the value would equal #last_id_in. How can I do something similar in PostgreSQL?
Depending on where you want your variable -- i.e. in a PL/pgSQL function, or in a script from which you're calling into Postgres...
PL/pgSQL, something like this:
INSERT INTO table2
SELECT id FROM table
WHERE col1 = 1
RETURNING id INTO last_id_in;
where last_id_in is previously declared as integer (or whatever type id is).
Script:
INSERT INTO table2
SELECT id FROM table1
WHERE col1 = 1
RETURNING id;
And then the cursor in whatever library you're interacting with Postgres with should contain that value for you to use elsewhere.
If the goal is simply to insert an element from one table into another, then the script version above can leave off the RETURNING clause, as there's no intervening steps.
Related
I'm trying to update a certain column of certain row WHERE id is certain value. The thing is, the number/names of columns are variable, and so are their respective ids.
For example:
UPDATE table SET column1="hello" WHERE id = 5
UPDATE table SET column2="cucumber" WHERE id = 6
How can I do a single mysql query in PDO to do this?
First thing I tried is...
UPDATE table SET column1="hello", column4="bye" WHERE id IN(5, 6)
But that query will update BOTH of those columns in rows where it finds BOTH of those ids, and that's not what I'm looking for. Is it only possible to do this query by query?
Keep in mind that the argument after SET is variable, so the columns to be updated, their values and their respective ids are also variable.
A solution where you can just purely bind values would be great, but if I have to build the query string with escaped variables, then that's OK too.
Thank you.
You can do this
UPDATE table t1 JOIN table t2
ON t1.id= 5 AND t2.id= 6
SET t1.column1= 'hello',
t2.column2 = 'cucumber';
Or if you want to do this on a single column
UPDATE table
SET column2 = CASE id
WHEN 5 THEN 'hello'
WHEN 6 THEN ''
END
WHERE id IN(5, 6);
i am trying to add incremented values to a new column in table.
Here is a sample structure of table
---------------------
Name - class - id
---------------------
abbc - 2 - null
efg - 4 - null
ggh - 6 - null
---------------------
i want to write a query that will generate unique id's for all records in table
Here is the query i have tried but show null
set #i=0;
update table1 set id =(#i:=#i+1);
What you have shown should work; the id column should be getting assigned values.
I tested your statement; I verified it works on my database. Here's the test case I ran:
CREATE TABLE table1 (`name` VARCHAR(4), class TINYINT, id INT);
INSERT INTO table1 (`name`,class) VALUES ('abbc',2),('efg',4),('ggh',6);
SET #i=0;
UPDATE table1 SET id =(#i:=#i+1);
SELECT * FROM table1;
Note that MySQL user variables are specific to a database session. If the SET is running in one session, and the UPDATE is running another session, that would explain the behavior you are seeing. (You didn't mention what client you ran the statements from; most clients reuse the same connection, and don't churn connections for each statement, I'm just throwing that out as a possibility.)
To insure that #i variable is actually initialized when the UPDATE statement runs, you can do the initialization in the UPDATE statement by doing something like this:
UPDATE table1 t
CROSS
JOIN (SELECT #i := 0) s
SET t.id =(#i:=#i+1);
I tested that, and that also works on my database.
try this query my friend:
set #i=0;
update table1 set id =(select #i:=#i+1);
SQL Fiddle
SET #a = 0;
UPDATE table_name SET id = #a:=#a+1;
Use AUTOINCREMENT parameter for the respective column instead. This parameter will put an unique auto incremented value in the respective column.
Trying to merge two game servers' tables that have this structure:
map | authid | name | time | date | ...
I would like to replace a row only if the time value of table2 is less than that of table1 AND ONLY if the map and authid values are BOTH the same. If the time values in table2 are greater, then the row from the current table (table1) should be kept untouched. Otherwise (on different map or authid values), the row from table2 should simply be appended to table1.
My way would be to
1st: create a view for replacing rows and another with the correct result set that should be appended like
--view for update
create view ChangeTable1
as
select table2.map, table2.authid,table2.name, table2.date, table1.map as t1map, table1.authid as t1authid...from table1 to inner join table2 on table1.map=table2.map and table1.athid=table2.autid
where table1.time>table2.time
-- view for appending
create view Add2Table1
select table2.map, table2.authid,table2.name, table2.date... from table2 where concat(table2.map, table2.authid) not in (select concat(table1.map, table1.authid) from table1)
-- update statement based on first view ChangeTable1
update ChangeTable1 set t1date=date, t1somevalue=somevalue......
-- insert Statement based an second view Add2Table1
insert into table 1 (map, authid, name, time, date, .... as select map, authid, name, time, date,... from Add2Table1
I hope this helps. I mostly do MS SQL, so there might be some syntax issues that need translation to MYSQL, Nils
If you need a permanent process doing this, you might consider putting this in a stored procedure
I have 2 tables. In table1 I have some rows for persons like this:
PersonX - ID
PersonX - Name
PersonX - Address
PersonY - ID
PersonY - AGE
In 2nd table, above mentioned ID, NAME,ADDRESS,AGE will be columns. And we have detailed data of personX and PersonY here.
Now, main issue is in stored procedure, using cursor, I am storing table1 values ('ID', 'Name', ...) in a variable #Element.
Now I am using select statement in same cursor as below:
SELECT #Element From Table2
I need output of user details like his id, age, address etc. But instead I am getting output as 'ID', 'NAME', 'AGE' etc....
I found that this is because #Element is varchar and has string value, so select statement is executed as below:
SELECT 'ID' from table2.
but all I need is like below
SELECT ID FROM TABLE2
I used replace function its not working for me. Case function, I can't use it because we can't say what data is there for a person in table1. It varies. I need one dynamic statement which can be use for all records. instead of executing case for each record.
SELECT REPLACE(#Element,'''','')
FROM TABLE2
(Still getting 'ID' as output, instead of corresponding value in Table2)
Please help me in this. Hope you understand my explanation
You will need to use dynamic SQL to achieve this, for example:
CREATE TABLE Table1
(
ID INT
,Name VARCHAR(255)
,ADDRESS VARCHAR(255)
)
INSERT Table1 VALUES ('1','Joe Bloggs','Address 1')
INSERT Table1 VALUES ('2','Jane Doe','Address 2')
DECLARE #ColName VARCHAR(255) = 'ID'
DECLARE #SQL VARCHAR(MAX)
SET #SQL = 'SELECT '+#ColName+','+''''+#ColName+''' FROM TABLE1'
EXEC (#SQL)
In the first column that is returned, is the SQL you want to be able to execute, however in the 2nd column in my query, is what you're doing at the moment. As far as SQL is concerned, your variable is simply storing a value. It does not know you are referring to a column name which is why it simply returns the value.
I want to do all these update in one statement.
update table set ts=ts_1 where id=1
update table set ts=ts_2 where id=2
...
update table set ts=ts_n where id=n
Is it?
Use this:
UPDATE `table` SET `ts`=CONCAT('ts_', `id`);
Yes you can but that would require a table (if only virtual/temporary), where you's store the id + ts value pairs, and then run an UPDATE with the FROM syntax.
Assuming tmpList is a table with an id and a ts_value column, filled with the pairs of id value, ts value you wish to apply.
UPDATE table, tmpList
SET table.ts = tmpList.ts_value
WHERE table.id = tmpList.id
-- AND table.id IN (1, 2, 3, .. n)
-- above "AND" is only needed if somehow you wish to limit it, i.e
-- if tmpTbl has more idsthan you wish to update
A possibly table-less (but similar) approach would involve a CASE statement, as in:
UPDATE table
SET ts = CASE id
WHEN 1 THEN 'ts_1'
WHEN 2 THEN 'ts_2'
-- ..
WHEN n THEN 'ts_n'
END
WHERE id in (1, 2, ... n) -- here this is necessary I believe
Well, without knowing what data, I'm not sure whether the answer is yes or no.
It certainly is possible to update multiple rows at once:
update table table1 set field1='value' where field2='bar'
This will update every row in table2 whose field2 value is 'bar'.
update table1 set field1='value' where field2 in (1, 2, 3, 4)
This will update every row in the table whose field2 value is 1, 2, 3 or 4.
update table1 set field1='value' where field2 > 5
This will update every row in the table whose field2 value is greater than 5.
update table1 set field1=concat('value', id)
This will update every row in the table, setting the field1 value to 'value' plus the value of that row's id field.
You could do it with a case statement, but it wouldn't be pretty:
UPDATE table
SET ts = CASE id WHEN 1 THEN ts_1 WHEN 2 THEN ts_2 ... WHEN n THEN ts_n END
I think that you should expand the context of the problem. Why do you want/need all the updates to be done in one statement? What benefit does that give you? Perhaps there's another way to get that benefit.
Presumably you are interacting with sql via some code, so certainly you can simply make sure that the three updates all happen atomically by creating a function that performs all three of the updates.
e.g. pseudocode:
function update_all_three(val){
// all the updates in one function
}
The difference between a single function update and some kind of update that performs multiple updates at once is probably not a very useful distinction.
generate the statements:
select concat('update table set ts = ts_', id, ' where id = ', id, '; ')
from table
or generate the case conditions, then connect it to your update statement:
select concat('when ', id, ' then ts_', id) from table
You can use INSERT ... ON DUPLICATE KEY UPDATE. See this quesion: Multiple Updates in MySQL
ts_1, ts_2, ts_3, etc. are different fields on the same table? There's no way to do that with a single statement.