I want to use Truncate command with Select * query . I am thinking if something like a nested query is applicable just to determine if the returned column is a decimal value or not and apply truncate (2 decimal point) to it.
A random version would look something like following -
Select * from my_Table ( Truncate if column = intiger)
Please provide pointers as on how to develop such kind of Query.
Related
I am looking for some ideas or help on how I could make my query a little less resource intensive but I am not sure if there is an easier way to do what I am doing.
I have a search Store Proc that takes in a large number of parameters to search the database for different products. The user can search for multiple makes and models etc in the same search.
The way I work the search is the parameters are all organised and the call of the store procedure is produced and saved in the data base this is then called and the store procedure is executed and the results returned.
An example:
Make Ids selected : 1 ,2 ,3 ,4
Model IDs selected : 2,3,4
Proc call generated "Call my_store_prod ('1,2,3,4','2,3,4')"
This is saved in the database with a random string eg "wmF14ndfglq2p3yuMMM6cr" then this is used to call the store proc using $_Get['search_id'] www.site.com?search=wmF14ndfglq2p3yuMMM6cr
In the Store proc I then create some temp tables:
DROP TEMPORARY TABLE IF EXISTS tmp_manufacturer_id;
CREATE TEMPORARY TABLE tmp_manufacturer_id (manufacturer_id varchar(255));
/*MODEL_ID*/
DROP TEMPORARY TABLE IF EXISTS tmp_model_id;
CREATE TEMPORARY TABLE tmp_model_id (model_id varchar(255));
Then use a funtion to split the string of ids '1,2,3,4' and insert into the temp tables:
CALL sp_split(#manufacturer_id,'tmp_manufacturer_id');
/*MODEL_ID*/
CALL sp_split(#model_id,'tmp_model_id') ;
Then in the where clause I have something like this:
CASE WHEN #manufacturer_id = ''
THEN information_view.advert_id = information_view.advert_id
ELSE information_view.manufacturer_id IN (select * from tmp_manufacturer_id2)end
AND
CASE WHEN #model_id = ''
THEN information_view.advert_id = information_view.advert_id
ELSE information_view.model_id IN (select * from tmp_model_id2)end
This all is working fine but one of my problems is I need to return 7 Random "Featured" Adverts with the same search results.
The only that I have thought of is to do a UNION with the same query only with a
order by rand()
LIMIT 7
I was wondering if there would be an easier way to get my 7 random adverts without having to duplicate the whole query?
Any pointers for be really useful.
Server version: 5.7.37-log - MySQL Community Server (GPL)
I have a MariaDB database where I've created a new column whose individual record values I'd like to populate with the results of multiplying the average of a previous column and the individual values from yet another column. All such columns are from the same table in the same database.
Here is the code I proposed to MariaDB, but I got an "Invalid use of group function" error:
UPDATE myTable SET new_column = (AVG(col_1)) * col_2;
My goal is for the above code to be able to accomplish what the below Excel formula would accomplish if it were entered in the 'C' column in a typical Excel spreadsheet and populated down the length of the table:
=(AVG(A1:A7)*B1)
Are there any thoughts if this can be done in MariaDB? All I found online was how to structure functions that return a single value to standard output, but not execute such a calculation down a column using an existing MariaDB function like 'AVG()' in the function.
I think this is what you are describing:
UPDATE myTable t CROSS JOIN
(SELECT AVG(col_1) as avg_col_1
FROM t
) ta
SET t.new_column = ta.avg_col_1 * col_2;
i have to compare two strings in a query like following:
SELECT *
FROM MY_TABLE
WHERE column LIKE '%keyword%';
But i want to compare unaccented values of both column and keyword. Is there an unaccent() function or other way to achieve this in MySQL?
AFAIK, No there is no unaccent() function present in MySQL. To ignore the accent you will have to set the proper collation for the column you are trying to compare. Example: How to remove accents in MySQL?
I have a table called table1 with three columns, one of which is Date_Of_Call which is of datetime type with the data in PDT. I basically need to convert the data from PDT to UTC and put the UTC converted dates into a new column in the existing table. I added a new column with:
alter table table1 ADD Date_Of_Call_UTC DATETIME;
I am able to get the proper time conversion with this select statement:
select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1;
The issue I am having is trying to use an update command to take the results of the select statement and put them in the new Date_Of_Call_UTC column. Any thoughts of how to do this?
I tried the below statement and a few variations but can't quite figure out what I need to do:
update table1 set table1.Date_Of_Call_UTC = (select CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00') from table1);
Any assistance is appreciated!
this one should work:
update table1
set table1.Date_Of_Call_UTC = CONVERT_TZ(Date_Of_Call, '-7:00', '-0:00');
NOTE: dates are usually stored already as UTC in mysql, but during output they can be displayed with offset applied, read about it: http://dev.mysql.com/doc/refman/5.0/en/datetime.html and http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
i have 10 tables with same structure except table name.
i have a sp (stored procedure) defined as following:
select * from table1 where (#param1 IS NULL OR col1=#param1)
UNION ALL
select * from table2 where (#param1 IS NULL OR col1=#param1)
UNION ALL
...
...
UNION ALL
select * from table10 where (#param1 IS NULL OR col1=#param1)
I am calling the sp with the following line:
call mySP('test') //it executes in 6,836s
Then I opened a new standard query window. I just copied the query above. Then replaced #param1 with 'test'.
This executed in 0,321s and is about 20 times faster than the stored procedure.
I changed the parameter value repeatedly for preventing the result to be cached. But this did not change the result. The SP is about 20 times slower than the equivalent standard query.
Please can you help me to figure out why this is happening ?
Did anybody encounter similar issues?
I am using mySQL 5.0.51 on windows server 2008 R2 64 bit.
edit: I am using Navicat for test.
Any idea will be helpful for me.
EDIT1:
I just have done some test according to Barmar's answer.
At finally i have changed the sp like below with one just one row:
SELECT * FROM table1 WHERE col1=#param1 AND col2=#param2
Then firstly i executed the standart query
SELECT * FROM table1 WHERE col1='test' AND col2='test' //Executed in 0.020s
After i called the my sp:
CALL MySp('test','test') //Executed in 0.466s
So i have changed where clause entirely but nothing changed. And i called the sp from mysql command window instead of navicat. It gave same result. I am still stuck on it.
my sp ddl:
CREATE DEFINER = `myDbName`#`%`
PROCEDURE `MySP` (param1 VARCHAR(100), param2 VARCHAR(100))
BEGIN
SELECT * FROM table1 WHERE col1=param1 AND col2=param2
END
And col1 and col2 is combined indexed.
You could say that why dont you use standart query then? My software design is not proper for this. I must use stored procedure. So this problem is highly important to me.
EDIT2:
I have gotten query profile informations. Big difference is because of "sending data row" in SP Profile Information. Sending data part takes %99 of query execution time. I am doing test on local database server. I am not connecting from remote computer.
SP Profile Informations
Query Profile Informations
I have tried force index statement like below in my sp. But same result.
SELECT * FROM table1 FORCE INDEX (col1_col2_combined_index) WHERE col1=#param1 AND col2=#param2
I have changed sp like below.
EXPLAIN SELECT * FROM table1 FORCE INDEX (col1_col2_combined_index) WHERE col1=param1 AND col2=param2
This gave this result:
id:1
select_type=SIMPLE
table:table1
type=ref
possible_keys:NULL
key:NULL
key_len:NULL
ref:NULL
rows:292004
Extra:Using where
Then i have executed the query below.
EXPLAIN SELECT * FROM table1 WHERE col1='test' AND col2='test'
Result is:
id:1
select_type=SIMPLE
table:table1
type=ref
possible_keys:col1_co2_combined_index
key:col1_co2_combined_index
key_len:76
ref:const,const
rows:292004
Extra:Using where
I am using FORCE INDEX statement in SP. But it insists on not using index. Any idea? I think i am close to end :)
Just a guess:
When you run the query by hand, the expression WHERE ('test' IS NULL or COL1 = 'test') can be optimized when the query is being parsed. The parser can see that the string 'test' is not null, so it converts the test to WHERE COL1 = 'test'. And if there's an index on COL1 this will be used.
However, when you create a stored procedure, parsing occurs when the procedure is created. At that time, it doesn't know what #param will be, and has to implement the query as a sequential scan of the table.
Try changing your procedure to:
IF #param IS NULL
THEN BEGIN
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
...
END;
ELSE BEGIN
SELECT * FROM table1 WHERE col1 = #param
UNION ALL
SELECT * FROM table2 WHERE col1 = #param
...
END;
END IF;
I don't have much experience with MySQL stored procedures, so I'm not sure that's all the right syntax.
Possible character set issue? If your table character set is different from your database character set, this may be causing a problem.
See this bug report: http://bugs.mysql.com/bug.php?id=26224
[12 Nov 2007 21:32] Mark Kubacki Still no luck with 5.1.22_rc - keys
are ingored, query takes within a procedure 36 seconds and outside
0.12s.
[12 Nov 2007 22:30] Mark Kubacki After having changed charsets to UTF-8 (especially for the two used), which is used for the
connection anyways, keys are taken into account within the stored
procedure!
The question I cannot answer is: Why does the optimizer treat charset
conversions an other way within and outside stored procedures?
(Indeed, I might be wrong asking this.)
Interesting question, because I am fond of using stored procedures. Reason is maintenance and the encapsulation principle.
This is information I found:
http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html
It states that the query cache is not used for queries that
1. are a subquery that belong to an outer query, and
2. are executed within the body of a stored procedure, trigger or event.
This implies that it works as designed.
I had seen this behavior, but it wasn't related to the character set.
I had a table that held self-referencing hierarchical data (a parent with children, and some children had children of their own, etc.). Since the parent_id had to reference the primary id's (and the column specified a constraint to that effect), I couldn't set the parent id to NULL or 0 (zero) to disassociate a child from a parent, so I simply referenced it to itself.
When I went to run a stored procedure to perform the recursive query to find all children (at all levels) of a particular parent, the query took between 30 & 40 times as long to run. I found that altering the query used by the stored procedure to make sure it excluded the top-level parent record (by specifying WHERE parent_id != id) restored the performance of the query.
The stored procedure I'm using is based on the one shown in:
https://stackoverflow.com/questions/27013093/recursive-query-emulation-in-mysql.