#1093 - You can't specify target table 'rent' for update in FROM clause - mysql-error-1093

"UPDATE rent SET remaining='$remnow' WHERE date=(select max(date) from rent where shopno='$shopno')";

You don't need 'from rent' in your UPDATE statement.
http://www.w3schools.com/sql/sql_update.asp
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Related

How to read / list security labels on columns in postgreSQL

I've set up PostgreSQL Anonymizer on my database with security labels and everything works fine.
I'm trying to regularly ceck if there is missing security labels on the columns of my database to telle the developers to add them in the next release but I can't fin a way to read the security labels.
Can anyone know how to do this ?
EDIT on 10/11/2022
Thanks to #Shiva, I've end up doing this query :
select cl."oid", col.ordinal_position, col.table_schema, col.table_name, col.column_name
FROM information_schema.columns col
join pg_catalog.pg_class cl on cl.relname = col.table_name
WHERE col.table_schema = 'XXXX'
and not exists (select objoid FROM pg_seclabel where provider = 'anon' and objsubid = col.ordinal_position and objoid = cl."oid");
You have to query pg_seclabel catalog to get list of security labels.
SELECT objsubid, provider, label FROM pg_seclabel WHERE objoid::regclass = 'mytable'::regclass
objsubid is the column number whose corresponding column name can be found by querying information_schema.columns catalog.
SELECT column_name FROM information_schema.columns WHERE table_name = 'mytable' AND ordinal_position = <column_number>
You can combine the above two queries to find columns that do not have the required security labels.

Sybase SQL - Select Most recent record and line up with the values from previous record

I am trying the following query on data table listed below....Sybase does not allow row_number() function. Any suggestions would be very helpful:
select
a.item_number,
a.item_rate,
a.item_code,
a.effective_dt,
r_prev.effective_dt,
r_prev.item_rate,
r.item_code
from A a LEFT OUTER join
(SELECT item_number, item_rate, item_code,effective_dt
FROM A
) a_prev
ON a.item_number = a_prev.item_number
AND a.rating_eff_dt < a_prev.rating_eff_dt
order BY a.item_number, r.item_rate, r.item_code, a.effective_dt desc, a_prev.rating_eff_dt
You can use
Select Number(*),<column list>
from table
Thank You.

Execute Dynamic Select into string in Teradata

This is the MySQL query, now I need this below dynamic query to execute in TERADATA SQL.
set l_sql=concat('SELECT max(',l_rid_col,'), MAX(cid) INTO #c2, #c3 FROM ',p_database,'.',p_table);
SET l_rid = #c2;
SET l_cid = #c3;
And this update query:
update table_a
set row = ifnull(l_rid, 0),
column = ifnull(l_cid, 0)
where databasename = p_database
and tablename = p_table;
But In Teradata I tried this way:
update table_a as a
from (select max(l_rid) TR, MAX(l_cid) TCC
from DEVP.employees) as b
set a.row = b.TR, a.column = b.TCC
where a.databasename = 'DEVP'
and a.tablename = 'employees';
Please remove the alias name from the LHS of the update statement.
a.colA=b.colname should be colA=b.colname
I got the answer:
update table_a from (select max(l_rid) TR, MAX(l_cid) TCC from DEVP.employees )as b
set row= b.TR , column=b.TCC where databasename='DEVP' and tablename='employees';
ISSUE: I just removed the alias name in UPDATE. finally got it.

DB2 Query : insert data in history table if not exists already

I have History table and transaction table.....and reference table...
If status in reference table is CLOSE then take those record verify in History table if not there insert from transaction table..... wiring query like this .... checking better one... please advice.. this query can be used for huge data ?
INSERT INTO LIB1.HIST_TBL
( SELECT R.ACCT, R.STATUS, R.DATE FROM
LIB2.HIST_TBL R JOIN LIB1.REF_TBL C
ON R.ACCT = C.ACCT WHERE C.STATUS = '5'
AND R.ACCT NOT IN
(SELECT ACTNO FROM LIB1.HIST_TBL)) ;
If you're on a current release of DB2 for i, take a look at the MERGE statement
MERGE INTO hist_tbl H
USING (SELECT * FROM ref_tbl R
WHERE r.status = 'S')
ON h.actno = r.actno
WHEN NOT MATCHED THEN
INSERT (actno,histcol2, histcol3) VALUES (r.actno,r.refcol2,r.refcol3)
--if needed
WHEN MATCHED
UPDATE SET (actno,histcol2, histcol3) = (r.actno,r.refcol2,r.refcol3)

update data using loop in sql syntax

I work with postgreSQL
I want to update email of all my users using sql
I have a table named user that contains 500 users,
so I think that I should use a loop in my sql syntax
For example when the table contains 4 users, I want the email for these users to become :
user1#hotmail.fr
user2#hotmail.fr
user3#hotmail.fr
user4#hotmail.fr
in java it should be like this
String newValue=null;
for(int i=0;i<list.size();i++)
{
newValue="user"+i+"#hotmail.fr";
// make update
}
I think that I should use plsql syntax
updated :
I try without success with this code :
BEGIN
FOR r IN SELECT * from user_
LOOP
NEXT r;
UPDATE user_ SET emailaddress = CONCAT('user',r,'#hotmail.fr')
END LOOP;
END
I solved the problem using this query :
UPDATE user_ SET emailaddress='user' || col_serial || '#hotmail.fr' FROM
(SELECT emailaddress, row_number() OVER ( ORDER BY createdate) AS col_serial FROM user_ ORDER BY createdate) AS t1
WHERE user_.emailaddress=t1.emailaddress