SQL Inner Query In Replace Statement - sql

I would like to replace a certain part of a columns field with something else. The only issue is that what I've tried doesn't work. I'm using HeidiSQL
This is a overview of the database:
Database Overview
What I would like to do is for each user_id with field_id 523 I would like to replace 'centraal-zorgportaal.nl/afbeeldingen/' with '/profiles/(for each user id here)/'.
So the end result example after running the query will look like this:
'/profiles/711/logo.gif', but then for each user with a diffrent logo.gif.
Note: Can't post more than two links so removed the http etc. in front.
This is the query I've tried and dind't work:
update wp_bp_xprofile_data
set value = replace( value,
'http://www.centraal-zorgportaal.nl/afbeeldingen/',
'/profiles/' +
(select user_id
from wp_bp_xprofile_data
where user_id = #n := #n + 1 n) +
'/')
where field_id = 523
(Table name is: wp_bp_xprofile_data)
This is the error message I received:
Error message:
Could anyone explain why this doesn't work, how to fix it and the best way to approach this problem?

In MySQL/MariaDB, you cannot refer to the table being modified. From your description, you seem to want something like this:
update wp_bp_xprofile_data
set value = replace(value,
'http://www.centraal-zorgportaal.nl/afbeeldingen/',
concat('/profiles/', user_id, '/')
)
where field_id = 523;

Related

SQL query customized output

I have below query:
select 'my.MYNAME=' + name from hostnames;
my.MYNAME=abc
my.MYNAME=xyz
my.MYNAME=poi
The query is dynamic is gives3 result, it may give more result depending upon data.
I need following output:
my.MYNAME1=abc
my.MYNAME2=xyz
my.MYNAME3=poi
Numbers appending to MYNAME according to result it gives.
I have tried
select 'my.MYNAME={c}' + name from hostnames where (select count(*) as c from name);
but it is not working.
One way to go about it is:
SELECT CONCAT(CONCAT(CONCAT('my.MYNAME',ROWNUM),'='), name) FROM hostnames
DEMO

Enter Unspecified Number of Variables into Postgres Psycopg2 SQL query

I'm trying to retrieve some data from a postgresql database using psycogp2, and either exclude a variable number of rows or exclude none.
The code I have so far is:
def db_query(variables):
cursor.execute('SELECT * '
'FROM database.table '
'WHERE id NOT IN (%s)', (variables,))
This does partially work. E.g. If I call:
db_query('593')
It works. The same for any other single value. However, I cannot seem to get it to work when I enter more than one variable, eg:
db_query('593, 595')
I get the error:
psycopg2.DataError: invalid input syntax for integer: "593, 595"
I'm not sure how to enter the query correctly or amend the SQL query. Any help appreciated.
Thanks
Pass a tuple as it is adapted to a record:
query = """
select *
from database.table
where id not in %s
"""
var1 = 593
argument = (var1,)
print(cursor.mogrify(query, (argument,)).decode('utf8'))
#cursor.execute(query, (argument,))
Output:
select *
from database.table
where id not in (593)

LAST REG From a Query SQL

I'm trying to get the last record from this query but i don't know how to do it. I used ROW_NUMBER but my program (Protheus ADVPL) don't have resources to get the last line from a query
SELECT ROW_NUMBER() OVER (ORDER BY B1_MASTER, B1_COD) AS ID,
B1_COD,
B1_DESC,
B1_CATEG,
B1_MASTER,
A2_COMPRAD,
ISNULL((SELECT Sum(C6_QTDVEN * C6_PRCVEN)
FROM SC6010 SC6,
SF4010 SF4,
SC5010 SC5
WHERE C6_FILIAL = '01'
AND C6_PRODUTO = B1_COD
AND SC6.D_E_L_E_T_ <> '*'
AND C5_FILIAL = C6_FILIAL
AND C5_NUM = C6_NUM
AND C5_EMISSAO BETWEEN '20160401' AND '20160404'
AND C5_TIPO = 'N'
AND C5_MODAL = '2'
AND SC5.D_E_L_E_T_ <> '*'
(query have 106 lines so i ll not put everything)
I need the total records in a column, like this:
Tabela
What can i do?
Tks
You can use MAX(field) too.
But, you're using ADVPL, so you could use dbSeek instead to find the last RECNO.
So, using "work area" you can find the last record with this:
TRB->(RECCOUNT())
I changed ROW_NUMBER to ##ROWCOUNT and it works! Tks all

PL/SQL: Using a variable in UPDATEXML function

I need to update an XML node that contains a specific number. My query works if I hard code in the number (see figure 1), but I would like to make this dynamic by passing through a variable that contains a string (variableToBeReplaced). I currently wrote this (see figure 2) but it isn't reading the variable correctly so no changes are being made to the xml. Does anyone know how I can include a variable in an updatexml() function?
Figure 1
select updateXML(xmltype(xmlbod),'/LpnList/Lpn/LicensePlateNumber[text() = "12345"]','67890').getClobVal() doc
from myTable
where id = '1'
Figure 2
select updateXML(xmltype(xmlbod), '/LpnList/Lpn/LicensePlateNumber[text()= '|| variableToBeReplaced || ']','67890').getClobVal() doc
from myTable
where id = '1'
I was just missing "" around the variable.
select updateXML(xmltype(xmlbod), '/LpnList/Lpn/LicensePlateNumber[text()= "'|| variableToBeReplaced || '"]','67890').getClobVal() doc
from myTable
where id = '1'

Check if a value exists in a collection stored in XML data type column

I have an XML data type column called "tags".
In that, I am storing a collection, like so:
<ArrayOfString>
<string>personal</string>
<string>travel</string>
<string>gadgets</string>
<string>parenting</string>
</ArrayOfString>
I want to select all the rows, that have one of the values that I am looking for: for example, I want to select all rows in the table that have a tag "travel".
I know that this works, if I know the index of the value I am looking for:
select * from posts
where tags.value('(/ArrayOfString/string)[1]', 'nvarchar(1000)') = 'travel'
but this query works only if the tag "travel" is the 2nd item in the nodes. How do I check if a value exists, irrespective of the position it is in?
select *
from tags
where tags.exist('/ArrayOfString/string[. = "travel"]') = 1
Or like this if you want to check against a variable.
declare #Val varchar(10)
set #Val = 'travel'
select *
from tags
where tags.exist('/ArrayOfString/string[. = sql:variable("#Val")]') = 1
You can try something like this:
SELECT
*
FROM
dbo.Posts
WHERE
tags.exist('/ArrayOfString/string/text()[. = "travel"]') = 1
This will list all the rows that have "travel" in one of the strings in your XML