Auto increment temporary column in select statement MySQL 8 - mysql

Hello i use this type of sentence to return an auto increment column in my prepared statement selects
cnt := cnt + 1
SET #query = CONCAT('SELECT * FROM (SELECT (#cnt := #cnt + 1) AS id, a.idProducto idProducto, a.idExProducto idExterno, trim(a.descripcion) nombreProducto, trim(a.descripcionAlt) nombreLargoProducto, trim(a.serial) serial,',
' a.peso, a.volumen, a.IdTpoGrupo, trim(b.descripcion) nombreGrupo,',
' a.idTpoLinea, trim(c.descripcion) nombreLinea,',
' a.idTpoMarca, trim(d.descripcion) nombreMarca,',
' a.idTpoUnidad, trim(e.descripcion) nombreTipoUnidad,',
' CASE WHEN a._estado = 1 THEN ''true'' ELSE ''false'' END estado, ',
' g.nombreArchivo imagen ',
' FROM tblProducto a',
' INNER JOIN tblProducto_TpoGrupo b ON a.IdTpoGrupo = b.IdTpoGrupo',
' INNER JOIN tblProducto_TpoLinea c ON a.idTpoLinea = c.idTpoLinea',
' INNER JOIN tblProducto_TpoMarca d ON a.idTpoMarca = d.idTpoMarca',
' INNER JOIN tblTpoUnidadMedida e ON a.idTpoUnidad = e.idTpoUnidadMed ',
' LEFT JOIN tblProductoXImagen f ON a.idProducto = f.idProducto AND f._estado=1',
' LEFT JOIN tblImagen g on f.idImagen = g.idImagen AND g._estado=1',
' WHERE a._estado<2 ',whereLike,whereConcat, ' order by idProducto ) allrecords');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
that works perfect on MySQL 5, but now in MySQL 8 is deprecated and my transaction return a warning, but the warning stop the execution of my stored procedure, i want to know if there is a solution to can achieve this behavior in MySQL 8.
Thanks for your help.

You can use window functions. The logic you sem to want is:
row_number() over(order by idProducto) as id
This gives you an incrementing integer value, that starts at 1 and increments according to idProducto. If two rows in the resultset have the same idProducto, it is undefined which one will be ordered "first" (it is, however, guaranteed that they will not get the same row number) - in that case, you might want to add one or more columns to the order by clause, so you do get a predictable, stable result.

Related

How to improve slow query performance?

I have a multi-join query that targeting the hospital's chart database.
this takes 5~10 seconds or more.
This is the visual expain using mysql workbench.
The query is below.
select sc.CLIENT_ID as 'guardianId', sp.PET_ID as 'patientId', sp.NAME as 'petName'
, (select BW from syn_vital where HOSPITAL_ID = sp.HOSPITAL_ID and PET_ID = sp.PET_ID order by DATE, TIME desc limit 1) as 'weight'
, sp.BIRTH as 'birth', sp.RFID as 'regNo', sp.BREED as 'vName'
, (case when ss.NAME like '%fel%' or ss.NAME like '%cat%' or ss.NAME like '%pawpaw%' or ss.NAME like '%f' then '002'
when ss.NAME like '%canine%' or ss.NAME like '%dog%' or ss.NAME like '%can%' then '001' else '007' end) as 'sCode'
, (case when LOWER(replace(sp.SEX, ' ', '')) like 'male%' then 'M'
when LOWER(replace(sp.SEX, ' ', '')) like 'female%' or LOWER(replace(sp.SEX, ' ', '')) like 'fam%' or LOWER(replace(sp.SEX, ' ', '')) like 'woman%' then 'F'
when LOWER(replace(sp.SEX, ' ', '')) like 'c.m%' or LOWER(replace(sp.SEX, ' ', '')) like 'castratedmale' or LOWER(replace(sp.SEX, ' ', '')) like 'neutered%' or LOWER(replace(sp.SEX, ' ', '')) like 'neutrality%man%' or LOWER(replace(sp.SEX, ' ', '')) like 'M.N%' then 'MN'
when LOWER(replace(sp.SEX, ' ', '')) like 'woman%' or LOWER(replace(sp.SEX, ' ', '')) like 'f.s%' or LOWER(replace(sp.SEX, ' ', '')) like 'S%' or LOWER(replace(sp.SEX, ' ', '')) like 'neutrality%%' then 'FS' else 'NONE' end) as 'sex'
from syn_client sc
left join syn_tel st on sc.HOSPITAL_ID = st.HOSPITAL_ID and sc.CLIENT_ID = st.CLIENT_ID
inner join syn_pet sp on sc.HOSPITAL_ID = sp.HOSPITAL_ID and sc.FAMILY_ID = sp.FAMILY_ID and sp.STATE = 0
inner join syn_species ss on sp.HOSPITAL_ID = ss.HOSPITAL_ID and sp.SPECIES_ID = ss.SPECIES_ID
WHERE
trim(replace(st.NUMBER, '-','')) = '01099999999'
and trim(sc.NAME) = 'johndoe'
and sp.HOSPITAL_ID = 'HOSPITALID999999'
order by TEL_DEFAULT desc
I would like to know how to improve the performance of this complex query.
The most obvious performance killers in your query are the non-sargable criteria in your where clause.
trim(replace(st.NUMBER, '-','')) = '01099999999'
This cannot use any available index as you have applied a function to the column, which needs to be evaluated before the comparison can be made.
As suggested by Pham, you could change your criterion to -
st.number IN ('01099999999', '01-099-999-999', 'ALL_OTHERS_FORMAT_YOU_ACCEPTS...')
or better still would be to normalize the numbers before you store them (you can always apply formatting for display purposes), that way you know how to search the stored data. Strip all the hyphens and spaces from the existings numbers -
UPDATE syn_tel
SET number = REPLACE(REPLACE(number, '-',''), ' ', '')
WHERE number LIKE '% %' OR number LIKE '%-%';
Similarly for the next criterion -
trim(sc.NAME) = 'johndoe'
The name should be trimmed before being stored in the database so there is no need to trim it when searching it. Update already stored names to trim whitespace -
UPDATE syn_client
SET NAME = TRIM(NAME)
WHERE NAME LIKE ' %' OR NAME LIKE '% ';
Changing sp.HOSPITAL_ID = 'HOSPITALID999999' to sc.HOSPITAL_ID = 'HOSPITALID999999' will allow for the use of a composite index on syn_client (HOSPITAL_ID, name) assuming you drop the TRIM() from the previously discussed criterion.
The sorting in your sub-query for weight might be wrong -
order by DATE, TIME desc limit 1
presumably you want the most recent weight -
order by `DATE` desc, `TIME` desc limit 1
/* OR */
order by CONCAT(`DATE`, ' ', `TIME`) desc limit 1
order by DATE, TIME desc -- really? That's equivalent to date ASC, time DESC. If you want "newest first", then ORDER BY date DESC, time DESC. Furthermore, it is usually bad practice and clumsy to code when you have DATE and TIME in separate columns. Is there a strong reason for storing them separately? It is reasonably easy to split them apart in a SELECT.
Similarly, cleanse NUMBER and NAME when inserting.
This will make the first subquery much faster:
syn_vital needs INDEX(hostital_id, pet_id, date, time, BW)
LIKE with a leading wildcard (%) is slow, but you probably cannot avoid it in this case.
LOWER(replace(sp.SEX, ' ', '')) -- Cleanse the input during INSERT, not on output!.
LOWER(...) -- With a suitable COLLATION (eg, the default), calling LOWER is unnecessary.
Some of these 'composite' INDEXes may be useful:
ss: INDEX(HOSPITAL_ID, SPECIES_ID, NAME)
st: INDEX(HOSPITAL_ID, CLIENT_ID, NUMBER)
sp: INDEX(HOSPITAL_ID, PET_ID)
What table is TEL_DEFAULT in?
You may want to:
Create index on syn_client(hospital_id, name --,tel_default?)
Create index on syn_tel(hospital_id, client_id, number)
Create index on syn_pet(hospital_id, family_id, state)
Create index on syn_species(hospital_id, species_id)
Change your query to:
SELECT ...
FROM syn_client sc
INNER JOIN syn_tel st ON sc.hospital_id = st.hospital_id AND sc.client_id = st.client_id
INNER JOIN syn_pet sp ON sc.hospital_id = sp.hospital_id AND sc.family_id = sp.family_id AND sp.state = 0
INNER JOIN syn_species ss ON sp.hospital_id = ss.hospital_id AND sp.species_id = ss.species_id
WHERE st.number IN ('01099999999', '01-099-999-999', 'ALL_OTHERS_FORMAT_YOU_ACCEPTS...')
AND trim(sc.name) = 'johndoe' --sc.name = 'johndoe' with standardize data input
AND sc.hospital_id = 'HOSPITALID999999' --not sp.hospital_id
ORDER BY tel_default DESC;

MySql pivot table dynamic columns

I've got the following tables in my database:
this is the table km_kondomanager_millesimal_table_values
this is the table km_kondomanager_millesimal_table
The two tables are joined together by km_kondomanager_millesimal_table_value_table_id and km_kondomanager_millesimal_table_id
I've managed to creat a pivot table using the followinf query:
SELECT km_kondomanager_millesimal_table_value_building_id
,SUM(CASE WHEN km_kondomanager_millesimal_table_value_table_id = 9
THEN km_kondomanager_millesimal_table_millesimal_value END) tabella_gruppo_A
,SUM(CASE WHEN km_kondomanager_millesimal_table_value_table_id = 10
THEN km_kondomanager_millesimal_table_millesimal_value END) tabella_gruppo_B
FROM km_kondomanager_millesimal_table_values
WHERE km_kondomanager_millesimal_table_value_group_id = 15
GROUP BY km_kondomanager_millesimal_table_value_building_id
and this is how it looks like:
My problem is that I need to take the table name from km_kondomanager_millesimal_table and use it as column, these columns are not always the same as you can see in the immage there are other stored in the database. The query I've created requires to specify km_kondomanager_millesimal_table_value_table_id in the case and also to specify the column name in the example tabella_gruppo_A and tabella_gruppo_B. Can anyboby help me to achieve this? Also I need to convert the pit table to HTML table. Many thanks
I've managed to get it sorted using mysql stored procedure and this is the code:
BEGIN
SELECT
GROUP_CONCAT(
CONCAT("MAX(IF(km_kondomanager_millesimal_table_value_table_id='", km_kondomanager_millesimal_table_value_table_id, "',km_kondomanager_millesimal_table_millesimal_value ,NULL)) AS '", km_kondomanager_millesimal_table_name, "'"), "
"
)INTO #answers
FROM (
SELECT DISTINCT km_kondomanager_millesimal_table_value_table_id, km_kondomanager_millesimal_table_name FROM km_kondomanager_millesimal_table_values INNER JOIN km_kondomanager_millesimal_table
ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_table_id = km_kondomanager_millesimal_table. km_kondomanager_millesimal_table_id WHERE km_kondomanager_millesimal_table_value_group_id = 13
) A;
SET #query :=
CONCAT(
'SELECT km_kondomanager_millesimal_table_value_building_id, ', #answers,
' FROM km_kondomanager_millesimal_table_values WHERE km_kondomanager_millesimal_table_value_group_id = 13 GROUP BY km_kondomanager_millesimal_table_value_building_id'
);
PREPARE statement FROM #query;
EXECUTE statement;
END

MySQL condition depending on column value

So, I have more or less this structure of columns in my table:
Name Age Operator
---- --- --------
Jhon 35 >
Michael 30 =
Jess 27 <
Based on that I want to make a query like this
SELECT * FROM mytable WHERE Name = 'John' AND Age > 40
obviosly this will return no results, and thats fine, but my problem is that I want to use Jhon's "Operator" value (> in this case) to make that condition.
Is it possible?
Thank you!
You can simply do it like this:
SELECT
*
FROM Table1
WHERE Name = 'Jhon'AND CASE
WHEN Operator = '>' THEN Age > 10
WHEN Operator = '<' THEN Age < 10
WHEN Operator = '=' THEN Age = 10
END
see it working live in an sqlfiddle
You also could use MySQL's PREPARE and EXECUTE statements to make dynamic SQL.
SET #name = 'Jhon';
SET #operator = NULL;
SET #age = 10;
SELECT
Operator
INTO
#operator
FROM
Table1
WHERE
Name = #name;
SET #SQL = CONCAT(
"SELECT"
, " * "
, " FROM "
, " Table1 "
, " WHERE "
, " name = '", #name, "' AND age ", #operator, ' ' , #age
);
SELECT #SQL; # not needed but you can see the generated SQL code which will be executed
PREPARE s FROM #SQL;
EXECUTE s;
see demo https://www.db-fiddle.com/f/3Z59Lxaoy1ZXC4kdNCtpsr/1

MySQL Variable Substitution

I want to simplify the following using dynamic SQL like one could do in Transact SQL.
I want to do something like:
SET #s = replace(field_name, '_complete','')
and use #s instead of replace(field_name, '_complete','')
Please adive if possible and if so how.
My current code:
select distinct
if(instr(replace(field_name, '_complete',''),'_') <= 5
,left(replace(field_name, '_complete','')
,instr(replace(field_name, '_complete',''),'_') - 1
)
,replace(field_name, '_complete','')
) AS form_id ,replace(
if(instr(replace(field_name, '_complete',''),'_') <= 5,
mid(replace(field_name, '_complete',''),
instr(replace(field_name, '_complete',''),'_') + 1,
length(replace(field_name, '_complete','')) - instr(replace(field_name, '_complete',''),'_')
)
,replace(field_name, '_complete','')
),
'_',
' ') as form_name ,field_name from redcap_extract2use where field_name like '%_complete' order by 1;
The above would then be replaced with:
select distinct
if(instr(#s,'_') <= 5 ,left(#s,instr(#s,'_') - 1),#s ) AS form_id
,replace( if(instr(#s,'_') <= 5,
mid(#s,instr(#s,'_') + 1,length(#s) - instr(#s,'_')),#s), '_', ' ') as form_name
,field_name
from redcap_extract2use
where field_name like '%_complete'
order by 1;
and I would have an execute... to run the query
If I'm understanding your question correctly then you would want to use the PREPARE and the EXECUTE statements.
For example:
SET #s = replace(field_name, '_complete','');
PREPARE mystatement FROM
SELECT DISTINCT ...... ;
EXECUTE mystatement;

Correlation via primary key column

I have two tables with unequal rows of data that I need to correlate with via "3rd-party" column. This 3rd-party column is a DATETIME primary key for both tables.
Here are the tables
|XKey |XColumn |
|-------------------------------|
|2012-01-00 |0.17
|2012-02-00 |0.24
|2012-03-00 |0.33
|2012-04-00 |0.41
|--------------------------------
Y TABLE
=======
|YKey |YColumn |
|-------------------------------|
|2012-01-01 |0.21
|2012-01-15 |0.12
|2012-02-01 |0.26
|2012-02-15 |0.27
|2012-03-01 |0.14
|2012-03-15 |0.11
|2012-04-01 |0.74
|2012-04-15 |0.66
|2012-05-01 |0.14
|--------------------------------
The values in y table should correlate to corresponding values in the x table whose xkey value is less than or equal to the respective ykey values.
This prepared statement is simply yielding null! What could I be overlooking?
SET #sql_run = CONCAT(
'SET #correl = (SELECT (1/(?-1))*SUM(((a.'
,XColumn
,' - ?)/?)*((b.'
,YColumn
,' - ?)/?)) FROM '
,XTable
,' a JOIN '
,YTable
,' b ON a.'
,XKey
,' <= b.'
,YKey
,' GROUP BY a.'
,XKey
,', b.'
,YKey
,' HAVING COUNT(a.'
,XKey
,') < 2 ORDER BY a.'
,XKey
,', b.'
,YKey
,')'
);
PREPARE statement_run FROM #sql_run ;
EXECUTE statement_run USING #num, #x_avg, #x_std, #y_avg, #y_std ;
DEALLOCATE PREPARE statement_run ;
Having adjuusted the date values in xtable am now getting the error 'subquery returns more than one row"
Your XKey date values are invalid. Try setting them to the first of the month instead of 0.