Using REGEXP_REPLACE in Update MariaDB - mariadb

Having this field in my database "description" (MariaDB, PHPMyAdmin):
<ul>
<li>Product 1 </li>
<li>Product 2</li>
</ul>
I want to update it to this (notice categoryName of IdProduct):
<ul>
<li>Product 1</li>
<li>Product 2</li>
</ul>
I have this query that picks up the pattern correctly (IdProduct = this pattern ([0-9]{1,5}) ) and the IdProduct correctly (second '\2') , the problem comes when making the subquery inside the concat expression since it doesn't pick up the value of IdProduct correctly ((first '\2') in subquery).
It works if I put a literal value. For example 1.
WHERE p3.id_product = 1
Here is my code:
UPDATE ps_product_lang pl
LEFT JOIN ps_product p ON (p.id_product = pl.id_product)
LEFT JOIN ps_category_lang cl2 ON (p.id_category_default = cl2.id_category)
SET pl.description = REGEXP_REPLACE(pl.description, '<a href="https://onedomain.com/(.*)-([0-9]{1,5})(?:-.*.html|.html)',
CONCAT('<a href="https://newdomain.com/'
, Replace((SELECT LOWER(cl3.name) FROM ps_product p3 LEFT JOIN ps_category_lang cl3 ON (cl3.id_category = p3.id_category_default)
WHERE p3.id_product = '\\2'), ' ', '-')
, '/' , '\\2', '-' , Replace(LOWER(pl.name), ' ', '-') , '.html'))
I tried with this and it does not work for me either:
WHERE p3.id_product = CAST('\\2' AS UNSIGNED)
I have also tried doing another subquey inside of WHERE p3.id_product = (subquery) but it returns only the first link found in the description (in this case productId=18) in all cases.
Here is my code and result:
UPDATE ps_product_lang pl
LEFT JOIN ps_product p ON (p.id_product = pl.id_product)
LEFT JOIN ps_category_lang cl2 ON (p.id_category_default = cl2.id_category)
SET pl.description = REGEXP_REPLACE(pl.description, '<a href="https://onedomain.com/(.*)-([0-9]{1,5})(?:-.*.html|.html)',
CONCAT('<a href="https://newdomain.com/'
, Replace((SELECT LOWER(cl3.name) FROM ps_product p3 LEFT JOIN ps_category_lang cl3 ON (cl3.id_category = p3.id_category_default)
WHERE p3.id_product =
(SELECT REGEXP_REPLACE(REGEXP_SUBSTR(pl2.description, '<a href="https://onedomain.com/(.*)-([0-9]{1,5})(?:-.*.html|.html)'), '<a href="https://onedomain.com/(.*)-([0-9]{1,5})(?:-.*.html|.html)', '\\2')
FROM ps_product_lang pl2 WHERE pl.id_product = pl2.id_product)), ' ', '-')
, '/' , '\\2', '-' , Replace(LOWER(pl.name), ' ', '-') , '.html'))
Result (notice both category's names match category-name-IdProduct18):
<ul>
<li>Product 1</li>
<li>Product 2</li>
</ul>

Related

HOW MAKE A update WITH left join DOCTRINE

i want to do this in doctrine symonfy2
update producto p
left join producto_compra pc ON p.id = pc.id_producto
set p.cantidad = (p.cantidad - pc.cantidad)
where pc.id_compra = '56';
and i tried with this, and it doesnt work
$em = $this->getEntityManager();
$query = $em->createQuery('UPDATE ORM\Entity\Producto p '
. 'LEFT JOIN ORM\Entity\ProductoCompra pc WITH p.id = pc.idProducto '
. 'SET p.cantidad = (p.cantidad - pc.cantidad) '
. 'WHERE pc.idCompra = :fc ')
->setParameter('fc', $facturacompra);
similar question look at here, i see that join is not supported in update or delete queries.

Operator "NOT LIKE" not work correctly

i am creating a new query in my app.
I use filters, with different options (Contain, equal or different).
If I use operator "different", my query generate like this:
SELECT p FROM ProductosBundle:Producto p
LEFT JOIN p.tipo t
LEFT JOIN p.departamento d
WHERE 1=1 AND t.nombre NOT LIKE '%articulo%'
I have product different from "articulo", but return 0 results...
I tried with NOT LIKE, != and <>, but not return results.
Is there something wrong in my condition where? I do not see the problem, with contain and equal it works.
Any idea? Thanks
EDIT 1:
This is my controller:
...
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT p FROM ProductosBundle:Producto p LEFT JOIN p.tipo t LEFT JOIN p.departamento d";
if (isset($_GET['filterField']) && isset($_GET['filterValue'])){
$valores = explode(",",$_GET['filterValue']);
$filas = explode(",",$_GET['filterField']);
$operadores = explode(",",$_GET['filterOperator']);
$dql .= " WHERE 1=1";
for($i = 0; $i < count($valores); $i++){
$dql.= " AND ". $filas[$i]. " " . $operadores[$i] ;
if($operadores[$i] == "LIKE" OR $operadores[$i] == "NOT LIKE"){
$dql .= " '%".$valores[$i]."%'";
}else{
$dql .= " '".$valores[$i]."'";
}
}
}
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$productos = $paginator->paginate(
$query,
$request->query->get('page', 1),
25
);
EDIT2:
My profiler is executing this:
SELECT DISTINCT p0_.id AS id_0
FROM producto p0_
LEFT JOIN tipo t1_ ON p0_.tipo = t1_.id
LEFT JOIN departamento d2_ ON p0_.departamento = d2_.id
WHERE t1_.nombre LIKE 'articulo'
AND 1 = 1
AND t1_.nombre NOT LIKE '%articulo%'
LIMIT 25 OFFSET 0
Its a LIKE 'Articulo'... WHY???!!!
Presumably, there's an error in your UI logic / the code setting the query parameters. This:
$valores = explode(",",$_GET['filterValue']);
$filas = explode(",",$_GET['filterField']);
$operadores = explode(",",$_GET['filterOperator']);
generates both
t1_.nombre LIKE 'articulo'
and
t1_.nombre NOT LIKE '%articulo%'
so you should take a look at the code that populates filterValue, filterField and filterOperator.

Coalesce and trim function

I am trying to execute the below script
CREATE VIEW JOES.WEBSKULOOKUPVIEW (
STYLEID ,
STYLENAME ,
DISPLAYSTYLENUMBER ,
B2BSTYLE ,
WEBPRODDESCR ,
WEBSTYLENAME ,
WEBSKUNUMBER ,
SUPPLIERNAME ,
SKUID ,
SKUNUMBER ,
SKUACTIVESTATUS ,
B2BSKU ,
SKUSIZE ,
SKUCOLOUR ,
WPSTYLEID )
AS
SELECT DISTINCT ESTYLE.STYLEID, ESTYLE.STYLENAME, ESTYLE.DISPLAYSTYLENBR, ESTYLE.B2BSTYLE, WPROD.H1SJXT, WPSTYLE.H2AMNA,
WPSKU.H3DINB, SUPP.SUPPLIERNAME, ESKU.SKUID, ESKU.SKUNUMBER, ESKU.SKUACTIVESTATUS, ESKU.B2BSKU,
(COALESCE(TRIM(FIRSTSIZE.SHORTSCALEVALUE) , '') || COALESCE(TRIM(', ' || SECSIZE.SHORTSCALEVALUE), '')) AS "SIZE",
STYLEACTUALCOLOUR.SPECIFICCOLOURNAME , STYLE.STYLEID
FROM ENTERPRISE.STYLE ESTYLE, ENTERPRISE.SUPPLIER SUPP, MWWDATA.DGH1CPP WPROD,
MWWDATA.DGH2CPP WPSTYLE, MWWDATA.DGH3CPP WPSKU, ENTERPRISE.STOCKKEEPINGUNIT ESKU, ENTERPRISE.STYLEACTUALCOLOUR STYLEACTUALCOLOUR,
ENTERPRISE.STYLE STYLE, ENTERPRISE.STYLEACTSTATUS STYLEACTSTATUS, ENTERPRISE.STYLEACTSTATUS SKUACTSTATUS, ENTERPRISE.SKUSIZE SKUSIZE
LEFT OUTER JOIN ENTERPRISE.SIZESCALEVALUE FIRSTSIZE ON FIRSTSIZESCALEID = FIRSTSIZE.SIZESCALEID AND FIRSTSIZESCALEOBJI = FIRSTSIZE.SIZESCALEOBJID
LEFT OUTER JOIN ENTERPRISE.SIZESCALEVALUE SECSIZE ON SECSIZESCALEID = SECSIZE.SIZESCALEID AND SECSIZESCALEOBJID = SECSIZE.SIZESCALEOBJID
WHERE ESTYLE.DEFAULTSUPPLIERID = SUPP.SUPPLIERID AND
SUPP.VENDORENTITYCODE = WPSTYLE.H2RWCO AND
ESTYLE.STYLEUSERCODE = WPSTYLE.H2AICD AND
WPSTYLE.H2SCNS = WPROD.H1SCNS AND
WPROD.H1SCNS = WPSTYLE.H2SCNS AND
WPSTYLE.H2SCNS = WPSKU.H3SCNS AND
WPSTYLE.H2RWCO = WPSKU.H3RWCO AND
WPSTYLE.H2AICD = WPSKU.H3AICD AND
WPSKU.H3DINB = CAST (ESKU.SKUNUMBER AS DECIMAL) AND
STYLE.STYLEID = ESKU.STYLEID AND
ESKU.STYLEID = STYLEACTUALCOLOUR.STYLEID AND
ESKU.ACTUALCOLOUROBJID = STYLEACTUALCOLOUR.ACTUALCOLOUROBJID AND
STYLE.STYLEACTIVESTATUS = STYLEACTSTATUS.STYLEACTIVESTATUS AND
ESKU.SKUACTIVESTATUS = SKUACTSTATUS.STYLEACTIVESTATUS AND
ESKU.SKUID = SKUSIZE.SKUID AND SKUACTIVESTATUS NOT IN ('P', 'D') ;
And it throws the error as mentioned below.
(COALESCE(TRIM(FIRSTSIZE.SHORTSCALEVALUE) , '') || COALESCE(TRIM(', ' || SECSIZE.SHORTSCALEVALUE), '')) AS "SIZE",
*
ERROR at line 20:
ORA-12704: character set mismatch
But that is the syntax for coalesce and trim.
So what possible changes should I make to solve this issue.

Stored Procedures and asp.net programmability; variable or SQL

Trying to display a users Lastname, Firstname --- Website
And I need to insert a comma and space after Lastname to a GridView.
I am trying to add a CASE statement in SQL and having trouble figuring it out.
Perhaps I need to use #parameter (scalar variable?) to abstract the
memory read from CASE statement; or my syntax is wrong and I just don't
understand.
SELECT
CASE
WHEN IsNull(people_Table.firstName, '') = ''
THEN CONCAT(people_Table.lastName, ', ', people_Table.firstName)
ELSE people_Table.lastName
END as fullName,
people_Table.website
FROM
people_Table
INNER JOIN
membership_Table on people_Table.ID = membership_Table.personID
WHERE
rectype = 'Master'
AND membershipType = 'Business'
AND expirationDate > GetDate()
ORDER BY
people_Table.lastName
Getting SQL Server error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'people_Table'.
Otherwise I suppose I should use an asp databoundevent in the template.
What is better for performance and security?
SELECT ISNULL(people_Table.lastName + ', ', '')
+ ISNULL(people_Table.firstName , '') as fullName
, people_Table.website
FROM people_Table INNER JOIN membership_Table on people_Table.ID =
membership_Table.personID
WHERE rectype = 'Master'
AND membershipType = 'Business'
AND expirationDate > GetDate()
ORDER BY people_Table.lastName
OR
SELECT COALESCE(people_Table.lastName + ', ', '')
+ COALESCE(people_Table.firstName , '') as fullName
, people_Table.website
FROM people_Table INNER JOIN membership_Table on people_Table.ID =
membership_Table.personID
WHERE rectype = 'Master'
AND membershipType = 'Business'
AND expirationDate > GetDate()
ORDER BY people_Table.lastName

Drupal db_query_range SQL query

I'm trying to get this working and the query executes but nothing comes back. I've tried everything I can think of, can you spot what I'm doing wrong?
$nido = $node->nid;
$result = db_query_range('
SELECT i.nid, i.iid, a.fid, p.filename, p.filepath
FROM {drup_image_attach} i
LEFT JOIN {drup_image} a ON i.iid = a.nid
LEFT JOIN {drup_files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nido, 0, 10);
echo "Filepath = " . $result ->filepath. "<br>";
echo "Filepath = " . $result ->filename . "<br>";
echo "IID = " . $result ->iid. "<br>";
echo "NID = " . $result ->nid . "<br>";
}
EDIT - I sorted out a couple of bits, but the output is still empty!
EDIT - this is the working code:
$nodeid = $node->nid;
$get_image = db_query('
SELECT p.filepath as imagefilename
FROM {image_attach} i
LEFT JOIN {image} a ON i.iid = a.nid
LEFT JOIN {files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nodeid);
$obj_image = db_fetch_object($get_image);
$imagefilename = $obj_image->imagefilename;
$result is a mysql(i) resource only. You first have to fetch the row/object.
$result = db_query_range(....);
$object = db_fetch_object($result);
print_r $object;
Assuming you have a range ob results not a single result this should work. You need to store the results in an array or process them in a different manner. for a single result see db_result()
$nodeid = $node->nid;
$get_image = db_query('
SELECT p.filepath as imagefilename
FROM {image_attach} i
LEFT JOIN {image} a ON i.iid = a.nid
LEFT JOIN {files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nodeid);
while(($obj_image = db_fetch_object($obj_image)) == TRUE){
$imagefilename[] = $obj_image->imagefilename;
}

Resources